alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Apache CXF example source code file (SortedArraySet.java)

This example Apache CXF source code file (SortedArraySet.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Apache CXF tags/keywords

atomicreference, iterator, object, object, sasiterator, sortedarrayset, sortedarrayset, sortedset, suppresswarnings, t, t, unsupportedoperationexception, util, x, x

The Apache CXF SortedArraySet.java source code

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.cxf.common.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.SortedSet;
import java.util.concurrent.atomic.AtomicReference;


/**
 * This class implements most of the <tt>Set interface, backed by a 
 * sorted Array.  This makes iterators very fast, lookups are log(n), but
 * adds are fairly expensive.
 * 
 * This class is also threadsafe, but without synchronizations.   Lookups
 * and iterators will iterate over the state of the Set when the iterator
 * was created.
 *
 * If no data is stored in the Set, it uses very little memory.  The backing
 * array is created on demand.
 * 
 * This class is primarly useful for stuff that will be setup at startup, but
 * then iterated over MANY times during runtime.
 *   
 * @param <T>
 */
public final class SortedArraySet<T> implements SortedSet {
    final AtomicReference<T[]> data = new AtomicReference();

    public void clear() {
        data.set(null);
    }

    public boolean isEmpty() {
        T[] tmp = data.get();
        return tmp == null || tmp.length == 0;
    }

    public Iterator<T> iterator() {
        return new SASIterator<T>(data.get());
    }

    public int size() {
        T[] tmp = data.get();
        return tmp == null ? 0 : tmp.length;
    }
    
    @SuppressWarnings("unchecked")
    private T[] newArray(int size) {
        return (T[])new Object[size];
    }
    
    public boolean add(T o) {
        if (!contains(o)) {
            T[] tmp = data.get();
            T[] tmp2;
            if (tmp == null) {
                tmp2 = newArray(1);
                tmp2[0] = o;
            } else {
                tmp2 = newArray(tmp.length + 1);
                System.arraycopy(tmp, 0, tmp2, 0, tmp.length);
                tmp2[tmp2.length - 1] = o;
                Arrays.sort(tmp2);
            }
            
            if (!data.compareAndSet(tmp, tmp2)) {
                return add(o);
            }
            return true;
        }
        return false;
    }
    public boolean addAll(Collection<? extends T> c) {
        boolean val = false;
        for (T t : c) {
            val |= add(t);
        }
        return val;
    }
    public boolean containsAll(Collection<?> c) {
        boolean val = false;
        for (Object t : c) {
            val |= contains(t);
        }
        return val;
    }

    public boolean contains(Object o) {
        T[] tmp = data.get();
        if (tmp == null) {
            return false;
        }
        return Arrays.binarySearch(tmp, o) >= 0;
    }

    public boolean removeAll(Collection<?> c) {
        boolean val = false;
        for (Object t : c) {
            val |= remove(t);
        }
        return val;
    }
    public boolean retainAll(Collection<?> c) {
        boolean val = false;
        for (T t : this) {
            if (!c.contains(t)) {
                val |= remove(t);
            }
        }
        return val;
    }

    public boolean remove(Object o) {
        T[] tmp = data.get();
        
        if (tmp == null) {
            return false;
        }
        int idx = Arrays.binarySearch(tmp, o);
        if (idx != -1) {
            if (tmp.length == 1 
                && !data.compareAndSet(tmp, null)) {
                return remove(o);
            }
            T[] tmp2 = newArray(tmp.length - 1);
            System.arraycopy(tmp, 0, tmp2, 0, idx);
            System.arraycopy(tmp, idx + 1, tmp2, idx, tmp.length - 1 - idx);
            if (!data.compareAndSet(tmp, tmp2)) {
                return remove(o);
            }
            return true;
        }
        return false;
    }


    public Object[] toArray() {
        T[] tmp = data.get();
        if (tmp == null) {
            return new Object[0];
        }
        T[] tmp2 = newArray(tmp.length);
        System.arraycopy(tmp, 0, tmp2, 0, tmp.length);
        return tmp2;
    }

    @SuppressWarnings("unchecked")
    public <X> X[] toArray(X[] a) {
        T[] tmp = data.get();
        if (tmp == null) {
            if (a.length != 0) {
                return (X[])java.lang.reflect.Array.
                    newInstance(a.getClass().getComponentType(), 0);
            }
            return a;
        }
        
        if (a.length < tmp.length) {
            a = (X[])java.lang.reflect.Array.
                newInstance(a.getClass().getComponentType(), tmp.length);
        }
        System.arraycopy(tmp, 0, a, 0, tmp.length);
        if (a.length > tmp.length) {
            a[tmp.length] = null;
        }
        return a;
    }

    @SuppressWarnings("unchecked")
    public boolean equals(Object o) {
        if (!(o instanceof SortedArraySet)) {
            return false;
        }
        SortedArraySet<T> as = (SortedArraySet)o;
        return Arrays.equals(data.get(), as.data.get());
    }
    public String toString() {
        return Arrays.toString(data.get());
    }
    public int hashCode() {
        return Arrays.hashCode(data.get()); 
    }
    

    private class SASIterator<X> implements Iterator {
        final X[] data;
        int idx;
        
        public SASIterator(X[] d) {
            data = d;
        }
        
        public boolean hasNext() {
            return data != null && idx != data.length;
        }

        public X next() {
            if (data == null || idx == data.length) {
                throw new NoSuchElementException();
            }
            return data[idx++];
        }

        public void remove() {
            if (idx > 0) {
                SortedArraySet.this.remove(data[idx - 1]);
            }
        }
    }


    public Comparator<? super T> comparator() {
        return null;
    }

    public T first() {
        T[] tmp = data.get();
        if (tmp == null || tmp.length == 0) {
            return null;
        }
        return tmp[0];
    }
    
    public T last() {
        T[] tmp = data.get();
        if (tmp == null || tmp.length == 0) {
            return null;
        }
        return tmp[tmp.length - 1];
    }

    public SortedSet<T> headSet(T toElement) {
        throw new UnsupportedOperationException();
    }

    public SortedSet<T> subSet(T fromElement, T toElement) {
        throw new UnsupportedOperationException();
    }

    public SortedSet<T> tailSet(T fromElement) {
        throw new UnsupportedOperationException();
    }

}

Other Apache CXF examples (source code examples)

Here is a short list of links related to this Apache CXF SortedArraySet.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.