You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/03/16 17:16:58 UTC
svn commit: r754926 [4/38] - in /incubator/pivot/tags/v1.0: ./ charts-test/
charts-test/src/ charts-test/src/pivot/ charts-test/src/pivot/charts/
charts-test/src/pivot/charts/test/ charts/ charts/lib/ charts/src/
charts/src/pivot/ charts/src/pivot/char...
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/ArrayQueue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/ArrayQueue.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/ArrayQueue.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/ArrayQueue.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+/**
+ * Implementation of the {@link Queue} interface that is backed by an
+ * array.
+ * <p>
+ * TODO The current implementation is not optimal, since it requires shifting
+ * all elements on every call to enqueue(). Use an approach that maintains
+ * rotating headIndex and tailIndex values and override List methods to use
+ * these as offsets from the current capacity value. When the capacity needs
+ * to increase, we'll copy the elements in a contiguous block to the new array
+ * (we may want to make this operation a protected method so ArrayList can call
+ * it polymorphically).
+ *
+ * @author gbrown
+ */
+public class ArrayQueue<T> extends ArrayList<T> implements Queue<T> {
+ public static final long serialVersionUID = 0;
+
+ public ArrayQueue() {
+ super();
+ }
+
+ public ArrayQueue(List<T> items) {
+ super(items);
+ }
+
+ public void enqueue(T item) {
+ insert(item, getComparator() == null ? 0 : -1);
+ }
+
+ public T dequeue() {
+ // TODO Throw if empty
+ return remove(getLength() - 1, 1).get(0);
+ }
+
+ public T peek() {
+ // TODO Return null if empty
+ return get(0);
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/ArrayStack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/ArrayStack.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/ArrayStack.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/ArrayStack.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+/**
+ * Implementation of the {@link Stack} interface that is backed by an
+ * array.
+ *
+ * @author gbrown
+ */
+public class ArrayStack<T> extends ArrayList<T> implements Stack<T> {
+ public static final long serialVersionUID = 0;
+
+ public ArrayStack() {
+ super();
+ }
+
+ public ArrayStack(List<T> items) {
+ super(items);
+ }
+
+ public void push(T item) {
+ insert(item, getComparator() == null ? getLength() : -1);
+ }
+
+ public T pop() {
+ int length = getLength();
+ if (length == 0) {
+ throw new IllegalStateException();
+ }
+
+ return remove(length - 1, 1).get(0);
+ }
+
+ public T peek() {
+ int length = getLength();
+
+ return (length == 0) ? null : get(length - 1);
+ }
+
+ public T poke(T item) {
+ int length = getLength();
+ if (length == 0) {
+ throw new IllegalStateException();
+ }
+
+ return update(length - 1, item);
+ }
+
+ public int getRemainingCapacity() {
+ return -1;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/Collection.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/Collection.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/Collection.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/Collection.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+
+/**
+ * Root interface in collection hierarchy. Defines operations common to all
+ * collections.
+ *
+ * @author gbrown
+ */
+public interface Collection<T> extends Iterable<T> {
+ /**
+ * Removes all elements from the collection.
+ */
+ public void clear();
+
+ /**
+ * Returns the collection's sort order.
+ *
+ * @return
+ * The comparator used to order elements in the collection, or <tt>null</tt>
+ * if the sort order is undefined.
+ *
+ * @see #setComparator(Comparator)
+ */
+ public Comparator<T> getComparator();
+
+ /**
+ * Sets the collection's sort order, re-ordering the collection's contents
+ * and ensuring that new entries preserve the sort order.
+ *
+ * @param comparator
+ * The comparator used to order elements in the collection, or null if the
+ * collection is unsorted.
+ */
+ public void setComparator(Comparator<T> comparator);
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/Dictionary.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/Dictionary.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/Dictionary.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/Dictionary.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+/**
+ * Interface representing a set of key/value pairs.
+ *
+ * @author gbrown
+ */
+public interface Dictionary<K, V> {
+ /**
+ * Retrieves the value for the given key.
+ *
+ * @param key
+ * The key whose value is to be returned.
+ *
+ * @return
+ * The value corresponding to <tt>key</tt>, or null if the key does not
+ * exist. Will also return null if the key refers to a null value.
+ * Use <tt>containsKey()</tt> to distinguish between these two cases.
+ */
+ public V get(K key);
+
+ /**
+ * Sets the value of the given key, creating a new entry or replacing the
+ * existing value.
+ *
+ * @param key
+ * The key whose value is to be set.
+ *
+ * @param value
+ * The value to be associated with the given key.
+ */
+ public V put(K key, V value);
+
+ /**
+ * Removes a key/value pair from the map.
+ *
+ * @param key
+ * The key whose mapping is to be removed.
+ *
+ * @return
+ * The value that was removed.
+ */
+ public V remove(K key);
+
+ /**
+ * Tests the existence of a key in the dictionary.
+ *
+ * @param key
+ * The key whose presence in the dictionary is to be tested.
+ *
+ * @return
+ * <tt>true</tt> if the key exists in the dictionary; <tt>false</tt>,
+ * otherwise.
+ */
+ public boolean containsKey(K key);
+
+ /**
+ * Tests the emptiness of the dictionary.
+ *
+ * @return
+ * <tt>true</tt> if the dictionary contains no keys; <tt>false</tt>,
+ * otherwise.
+ */
+ public boolean isEmpty();
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/EnumSet.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/EnumSet.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/EnumSet.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/EnumSet.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,56 @@
+package pivot.collections;
+
+import java.io.Serializable;
+import java.util.Comparator;
+import java.util.Iterator;
+
+import pivot.util.ListenerList;
+
+/**
+ * Implementation of the {@link Set} interface that is backed by a bitfield
+ * representing enum values. Enum values are mapped to the bitfield by their
+ * ordinal value.
+ */
+public class EnumSet<E extends Enum<E>> implements Set<E>, Serializable {
+ public static final long serialVersionUID = 0;
+
+ private int bitSet = 0;
+ private SetListenerList<E> setListeners = new SetListenerList<E>();
+
+ public void add(E element) {
+ bitSet |= (2 << element.ordinal());
+ }
+
+ public void remove(E element) {
+ bitSet &= ~(2 << element.ordinal());
+ }
+
+ public void clear() {
+ bitSet = 0;
+ }
+
+ public boolean contains(E element) {
+ return (bitSet & (2 << element.ordinal())) > 0;
+ }
+
+ public boolean isEmpty() {
+ return bitSet > 0;
+ }
+
+ public ListenerList<SetListener<E>> getSetListeners() {
+ return setListeners;
+ }
+
+ public Comparator<E> getComparator() {
+ return null;
+ }
+
+ public void setComparator(Comparator<E> comparator) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Iterator<E> iterator() {
+ // TODO
+ return null;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/Group.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/Group.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/Group.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/Group.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+/**
+ * Interface representing a group of unique elements.
+ *
+ * @author gbrown
+ */
+public interface Group<E> {
+ /**
+ * Adds an element to the group.
+ *
+ * @param element
+ * The element to add to the group.
+ */
+ public void add(E element);
+
+ /**
+ * Removes an element from the group.
+ *
+ * @param element
+ * The element to remove from the set.
+ */
+ public void remove(E element);
+
+ /**
+ * Tests the existence of an element in the group.
+ *
+ * @param element
+ * The element whose presence in the group is to be tested.
+ *
+ * @return
+ * <tt>true</tt> if the element exists in the group; <tt>false</tt>,
+ * otherwise.
+ */
+ public boolean contains(E element);
+
+ /**
+ * Tests the emptiness of the group.
+ *
+ * @return
+ * <tt>true</tt> if the group contains no elements; <tt>false</tt>,
+ * otherwise.
+ */
+ public boolean isEmpty();
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/HashMap.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/HashMap.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/HashMap.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/HashMap.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.io.Serializable;
+import java.util.Comparator;
+import java.util.Iterator;
+
+import pivot.util.ImmutableIterator;
+import pivot.util.ListenerList;
+
+/**
+ * Implementation of the {@link Map} interface that is backed by a
+ * hashtable.
+ * <p>
+ * TODO We're temporarily using a java.util.HashMap to back this map.
+ * Eventually, we'll replace this with an internal map representation.
+ */
+public class HashMap<K, V> implements Map<K, V>, Serializable {
+ public static final long serialVersionUID = 0;
+
+ protected java.util.Map<K, V> hashMap = null;
+
+ private Comparator<K> comparator = null;
+ private transient MapListenerList<K, V> mapListeners = new MapListenerList<K, V>();
+
+ public HashMap() {
+ this(false, null);
+ }
+
+ public HashMap(boolean weak) {
+ this(weak, null);
+ }
+
+ public HashMap(Map<K, V> map) {
+ this(false, map);
+ }
+
+ public HashMap(boolean weak, Map<K, V> map) {
+ hashMap = (weak) ? new java.util.WeakHashMap<K, V>() : new java.util.HashMap<K, V>();
+
+ if (map != null) {
+ for (K key : map) {
+ put(key, map.get(key));
+ }
+ }
+ }
+
+ public HashMap(Comparator<K> comparator) {
+ // TODO
+ throw new UnsupportedOperationException("HashMap auto-sorting is not yet supported.");
+
+ // this.comparator = comparator;
+ }
+
+ public V get(K key) {
+ return hashMap.get(key);
+ }
+
+ public V put(K key, V value) {
+ boolean update = hashMap.containsKey(key);
+ V previousValue = hashMap.put(key, value);
+
+ if (update) {
+ mapListeners.valueUpdated(this, key, previousValue);
+ }
+ else {
+ mapListeners.valueAdded(this, key);
+ }
+
+ return previousValue;
+ }
+
+ public V remove(K key) {
+ V value = null;
+
+ if (hashMap.containsKey(key)) {
+ value = hashMap.remove(key);
+ mapListeners.valueRemoved(this, key, value);
+ }
+
+ return value;
+ }
+
+ public void clear() {
+ hashMap.clear();
+ mapListeners.mapCleared(this);
+ }
+
+ public boolean containsKey(K key) {
+ return hashMap.containsKey(key);
+ }
+
+ public boolean isEmpty() {
+ return hashMap.isEmpty();
+ }
+
+ public Comparator<K> getComparator() {
+ return comparator;
+ }
+
+ public void setComparator(Comparator<K> comparator) {
+ // TODO
+ throw new UnsupportedOperationException("HashMap auto-sorting is not yet supported.");
+ }
+
+ public Iterator<K> iterator() {
+ // TODO Return an iterator that supports modification?
+ return new ImmutableIterator<K>(hashMap.keySet().iterator());
+ }
+
+ public ListenerList<MapListener<K, V>> getMapListeners() {
+ return mapListeners;
+ }
+
+ public String toString() {
+ return hashMap.toString();
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/HashSet.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/HashSet.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/HashSet.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/HashSet.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.io.Serializable;
+import java.util.Comparator;
+import java.util.Iterator;
+
+import pivot.util.ImmutableIterator;
+import pivot.util.ListenerList;
+
+/**
+ * Implementation of the {@link Set} interface that is backed by a
+ * hashtable.
+ * <p>
+ * TODO We're temporarily using a java.util.HashSet to back this set.
+ * Eventually, we'll replace this with an internal set representation.
+ */
+public class HashSet<E> implements Set<E>, Serializable {
+ public static final long serialVersionUID = 0;
+
+ protected java.util.HashSet<E> hashSet = null;
+
+ private Comparator<E> comparator = null;
+ private transient SetListenerList<E> setListeners = new SetListenerList<E>();
+
+ public HashSet() {
+ hashSet = new java.util.HashSet<E>();
+ }
+
+ public HashSet(Set<E> set) {
+ hashSet = new java.util.HashSet<E>();
+
+ for (E element : set) {
+ add(element);
+ }
+ }
+
+ public HashSet(Comparator<E> comparator) {
+ throw new UnsupportedOperationException("HashSet auto-sorting is not yet supported.");
+
+ // this.comparator = comparator;
+ }
+
+ public void add(E element) {
+ if (!hashSet.contains(element)) {
+ hashSet.add(element);
+ setListeners.elementAdded(this, element);
+ }
+ }
+
+ public void remove(E element) {
+ if (hashSet.contains(element)) {
+ hashSet.remove(element);
+ setListeners.elementRemoved(this, element);
+ }
+ }
+
+ public void clear() {
+ hashSet.clear();
+ setListeners.setCleared(this);
+ }
+
+ public boolean contains(E element) {
+ return hashSet.contains(element);
+ }
+
+ public boolean isEmpty() {
+ return hashSet.isEmpty();
+ }
+
+ public Comparator<E> getComparator() {
+ return comparator;
+ }
+
+ public void setComparator(Comparator<E> comparator) {
+ // TODO
+ throw new UnsupportedOperationException("HashSet auto-sorting is not yet supported.");
+ }
+
+ public Iterator<E> iterator() {
+ // TODO Return an iterator that supports modification?
+ return new ImmutableIterator<E>(hashSet.iterator());
+ }
+
+ public ListenerList<SetListener<E>> getSetListeners() {
+ return setListeners;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedList.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedList.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedList.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+import java.util.Iterator;
+
+import pivot.util.ListenerList;
+
+/**
+ * Implementation of the {@link List} interface that is backed by a linked
+ * list.
+ * <p>
+ * TODO This class is currently incomplete.
+ */
+public class LinkedList<T> implements List<T> {
+ public int add(T item) {
+ // TODO
+ return 0;
+ }
+
+ public void insert(T item, int index) {
+ // TODO Auto-generated method stub
+ }
+
+ public T update(int index, T item) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int remove (T item) {
+ // TODO
+ return -1;
+ }
+
+ public Sequence<T> remove(int index, int count) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void clear() {
+ // TODO
+ }
+
+ public T get(int index) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int indexOf(T item) {
+ // TODO
+ return -1;
+ }
+
+ public int getLength() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public Comparator<T> getComparator() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void setComparator(Comparator<T> comparator) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public Iterator<T> iterator() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ListenerList<ListListener<T>> getListListeners() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedQueue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedQueue.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedQueue.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedQueue.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+/**
+ * Implementation of the {@link Queue} interface that is backed by a linked
+ * list.
+ * <p>
+ * TODO This class is currently incomplete.
+ */
+public class LinkedQueue<T> extends LinkedList<T> implements Queue<T> {
+
+ public void enqueue(T item) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public T dequeue() {
+ // TODO Auto-generated method stub
+ // TODO Throw if empty
+ return null;
+ }
+
+ public T peek() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedStack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedStack.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedStack.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/LinkedStack.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+/**
+ * Implementation of the {@link Stack} interface that is backed by a linked
+ * list.
+ * <p>
+ * TODO This class is currently incomplete.
+ */
+public class LinkedStack<T> extends LinkedList<T> implements Stack<T> {
+
+ public void push(T item) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public T pop() {
+ // TODO Auto-generated method stub
+ // TODO Throw if empty
+ return null;
+ }
+
+ public T peek() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public T poke(T item) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/List.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/List.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/List.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/List.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+import pivot.util.ListenerList;
+
+/**
+ * Collection interface representing an ordered sequence of items.
+ *
+ * @author gbrown
+ */
+public interface List<T> extends Sequence<T>, Collection<T> {
+ /**
+ * List listener list implementation.
+ *
+ * @author gbrown
+ */
+ public static class ListListenerList<T>
+ extends ListenerList<ListListener<T>> implements ListListener<T> {
+ public void itemInserted(List<T> list, int index) {
+ for (ListListener<T> listener : this) {
+ listener.itemInserted(list, index);
+ }
+ }
+
+ public void itemsRemoved(List<T> list, int index, Sequence<T> items) {
+ for (ListListener<T> listener : this) {
+ listener.itemsRemoved(list, index, items);
+ }
+ }
+
+ public void itemUpdated(List<T> list, int index, T previousItem) {
+ for (ListListener<T> listener : this) {
+ listener.itemUpdated(list, index, previousItem);
+ }
+ }
+
+ public void comparatorChanged(List<T> list, Comparator<T> previousComparator) {
+ for (ListListener<T> listener : this) {
+ listener.comparatorChanged(list, previousComparator);
+ }
+ }
+ }
+
+ /**
+ * Adds an item to the list. If the list is unsorted, the item is appended
+ * to the end of the list. Otherwise, it is inserted at the appropriate
+ * index.
+ *
+ * @see pivot.collections.ListListener#itemInserted(List, int)
+ *
+ * @return
+ * The index at which the item was added.
+ */
+ public int add(T item);
+
+ /**
+ * Inserts an item into the list.
+ *
+ * @param item
+ * The item to be added to the list.
+ *
+ * @param index
+ * The index at which the item should be inserted. Must be a value between
+ * <tt>0</tt> and <tt>getLength()</tt>.
+ *
+ * @throws IllegalArgumentException
+ * If the list is sorted and the insertion point of the item does not match
+ * the given index.
+ *
+ * @see ListListener#itemInserted(List, int)
+ */
+ public void insert(T item, int index);
+
+ /**
+ * Updates the item at the given index.
+ *
+ * @param index
+ * The index of the item to update.
+ *
+ * @param item
+ * The item that will replace any existing value at the given index.
+ *
+ * @throws IllegalArgumentException
+ * If the list is sorted and the index of the updated item would be
+ * different than its current index.
+ *
+ * @see ListListener#itemUpdated(List, int, Object)
+ */
+ public T update(int index, T item);
+
+ /**
+ * @see ListListener#itemsRemoved(List, int, Sequence)
+ */
+ public Sequence<T> remove(int index, int count);
+
+ /**
+ * @see ListListener#itemsRemoved(List, int, Sequence)
+ */
+ public void clear();
+
+ /**
+ * Returns the length of the list.
+ *
+ * @return The number of items in the list, or -1 if the list's length is
+ * not known. In this case, the iterator must be used to retrieve the
+ * contents of the list.
+ */
+ public int getLength();
+
+ /**
+ * @see ListListener#comparatorChanged(List, Comparator)
+ */
+ public void setComparator(Comparator<T> comparator);
+
+ /**
+ * Returns the list listener list.
+ */
+ public ListenerList<ListListener<T>> getListListeners();
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/ListListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/ListListener.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/ListListener.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/ListListener.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+
+/**
+ * List listener interface.
+ *
+ * @author gbrown
+ */
+public interface ListListener<T> {
+ /**
+ * Called when an item has been inserted into a list.
+ *
+ * @param list
+ * The source of the list event.
+ *
+ * @param index
+ * The index at which the item was added.
+ */
+ public void itemInserted(List<T> list, int index);
+
+ /**
+ * Called when items have been removed from a list.
+ *
+ * @param list
+ * The source of the list event.
+ *
+ * @param index
+ * The starting index from which items have been removed.
+ *
+ * @param items
+ * The items that were removed from the list, or <tt>null</tt> if the list
+ * was cleared.
+ */
+ public void itemsRemoved(List<T> list, int index, Sequence<T> items);
+
+ /**
+ * Called when a list item has been updated.
+ *
+ * @param list
+ * The source of the list event.
+ *
+ * @param index
+ * The index of the item that was updated.
+ *
+ * @param previousItem
+ * The item that was previously stored at <tt>index</tt>.
+ */
+ public void itemUpdated(List<T> list, int index, T previousItem);
+
+ /**
+ * Called when a list's comparator has changed.
+ *
+ * @param list
+ * The source of the event.
+ *
+ * @param previousComparator
+ * The previous comparator value.
+ */
+ public void comparatorChanged(List<T> list, Comparator<T> previousComparator);
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/Map.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/Map.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/Map.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/Map.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+
+import pivot.util.ListenerList;
+
+/**
+ * Collection interface representing set of key/value pairs.
+ *
+ * @author gbrown
+ */
+public interface Map<K, V> extends Dictionary<K, V>, Collection<K> {
+ /**
+ * Map listener list implementation.
+ *
+ * @author gbrown
+ */
+ public static class MapListenerList<K, V>
+ extends ListenerList<MapListener<K, V>> implements MapListener<K, V> {
+ public void valueAdded(Map<K, V> map, K key) {
+ for (MapListener<K, V> listener : this) {
+ listener.valueAdded(map, key);
+ }
+ }
+
+ public void valueRemoved(Map<K, V> map, K key, V value) {
+ for (MapListener<K, V> listener : this) {
+ listener.valueRemoved(map, key, value);
+ }
+ }
+
+ public void valueUpdated(Map<K, V> map, K key, V previousValue) {
+ for (MapListener<K, V> listener : this) {
+ listener.valueUpdated(map, key, previousValue);
+ }
+ }
+
+ public void mapCleared(Map<K, V> map) {
+ for (MapListener<K, V> listener : this) {
+ listener.mapCleared(map);
+ }
+ }
+
+ public void comparatorChanged(Map<K, V> map, Comparator<K> previousComparator) {
+ for (MapListener<K, V> listener : this) {
+ listener.comparatorChanged(map, previousComparator);
+ }
+ }
+ }
+
+ /**
+ * Sets the value of the given key, creating a new entry or replacing the
+ * existing value, and firing a corresponding event.
+ *
+ * @param key
+ * The key whose value is to be set.
+ *
+ * @param value
+ * The value to be associated with the given key.
+ *
+ * @see MapListener#valueAdded(Map, Object)
+ * @see MapListener#valueUpdated(Map, Object, Object)
+ */
+ public V put(K key, V value);
+
+ /**
+ * @see MapListener#valueRemoved(Map, Object, Object)
+ */
+ public V remove(K key);
+
+ /**
+ * Removes all entries in the map.
+ *
+ * @see MapListener#mapCleared(Map)
+ */
+ public void clear();
+
+ /**
+ * @see MapListener#comparatorChanged(Map, Comparator)
+ */
+ public void setComparator(Comparator<K> comparator);
+
+ /**
+ * Returns the map listener collection.
+ */
+ public ListenerList<MapListener<K, V>> getMapListeners();
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/MapListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/MapListener.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/MapListener.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/MapListener.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+
+/**
+ * Map listener interface.
+ *
+ * @author gbrown
+ */
+public interface MapListener<K, V> {
+ /**
+ * Called when a key/value pair has been added to a map.
+ *
+ * @param map
+ * The source of the map event.
+ *
+ * @param key
+ * The key that was added to the map.
+ */
+ public void valueAdded(Map<K, V> map, K key);
+
+ /**
+ * Called when a map value has been updated.
+ *
+ * @param map
+ * The source of the map event.
+ *
+ * @param key
+ * The key whose value was updated.
+ *
+ * @param previousValue
+ * The value that was previously associated with the key.
+ */
+ public void valueUpdated(Map<K, V> map, K key, V previousValue);
+
+ /**
+ * Called when a key/value pair has been removed from a map.
+ *
+ * @param map
+ * The source of the map event.
+ *
+ * @param key
+ * The key that was removed.
+ *
+ * @param value
+ * The value that was removed.
+ */
+ public void valueRemoved(Map<K, V> map, K key, V value);
+
+ /**
+ * Called when map data has been reset.
+ *
+ * @param map
+ * The source of the map event.
+ */
+ public void mapCleared(Map<K, V> map);
+
+ /**
+ * Called when a map's comparator has changed.
+ *
+ * @param map
+ * The source of the event.
+ *
+ * @param previousComparator
+ * The previous comparator value.
+ */
+ public void comparatorChanged(Map<K, V> map, Comparator<K> previousComparator);
+}
+
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/Queue.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/Queue.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/Queue.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/Queue.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+/**
+ * Interface representing a first-in, first-out (FIFO) queue when unsorted, and
+ * a priority queue when sorted.
+ *
+ * @author gbrown
+ */
+public interface Queue<T> extends List<T> {
+ /**
+ * Enqueues an item. If the queue is unsorted, the item is added at the
+ * tail of the queue (index <tt>0</tt>). Otherwise, it is inserted at the
+ * appropriate index.
+ *
+ * @param item
+ * The item to add to the queue.
+ */
+ public void enqueue(T item);
+
+ /**
+ * Removes the item from the head of the queue and returns it. Calling this
+ * method should have the same effect as:
+ *
+ * <code>remove(getLength() - 1, 1);</code>
+ */
+ public T dequeue();
+
+ /**
+ * Returns the item at the head of the queue without removing it from the
+ * queue. Returns null if the queue contains no items. Will also return null
+ * if the head item in the queue is null. <tt>getLength()</tt> can be used
+ * to distinguish between these two cases.
+ */
+ public T peek();
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/Sequence.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/Sequence.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/Sequence.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/Sequence.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,549 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+
+/**
+ * Interface representing an ordered sequence of items.
+ *
+ * @author gbrown
+ */
+public interface Sequence<T> {
+ /**
+ * Collection of static utility methods providing path access to nested
+ * sequence data.
+ *
+ * @author gbrown
+ */
+ public static class Tree {
+ /**
+ * Adds an item to a nested sequence.
+ *
+ * @param sequence
+ * The root sequence.
+ *
+ * @param item
+ * The item to be added to the sequence.
+ *
+ * @param path
+ * The path of the sequence to which the item should be added.
+ *
+ * @return
+ * The index at which the item was inserted, relative to the parent
+ * sequence.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> int add(Sequence<T> sequence, T item, Sequence<Integer> path) {
+ return ((Sequence<T>)get(sequence, path)).add(item);
+ }
+
+ /**
+ * Inserts an item into a nested sequence.
+ *
+ * @param sequence
+ * The root sequence.
+ *
+ * @param item
+ * The item to be inserted into the sequence.
+ *
+ * @param path
+ * The path of the sequence into which the item should be inserted.
+ *
+ * @param index
+ * The index at which the item should be inserted within the parent
+ * sequence.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> void insert(Sequence<T> sequence, T item, Sequence<Integer> path,
+ int index) {
+ ((Sequence<T>)get(sequence, path)).insert(item, index);
+ }
+
+ /**
+ * Updates an item in a nested sequence.
+ *
+ * @param sequence
+ * The root sequence.
+ *
+ * @param path
+ * The path of the item to update.
+ *
+ * @param item
+ * The item that will replace any existing value at the given path.
+ *
+ * @return
+ * The item that was previously stored at the given path.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T update(Sequence<T> sequence, Sequence<Integer> path, T item) {
+ if (sequence == null) {
+ throw new IllegalArgumentException("sequence is null.");
+ }
+
+ if (path == null) {
+ throw new IllegalArgumentException("path is null.");
+ }
+
+ int i = 0, n = path.getLength() - 1;
+ while (i < n) {
+ sequence = (Sequence<T>)sequence.get(path.get(i++));
+ }
+
+ return sequence.update(path.get(i), item);
+ }
+
+ /**
+ * Removes the first occurrence of an item from a nested sequence.
+ *
+ * @param sequence
+ * The root sequence.
+ *
+ * @param item
+ * The item to remove.
+ *
+ * @return
+ * The path of the item that was removed.
+ */
+ public static <T> Sequence<Integer> remove(Sequence<T> sequence, T item) {
+ Sequence<Integer> path = pathOf(sequence, item);
+ if (path == null) {
+ throw new IllegalArgumentException("item is not a descendant of sequence.");
+ }
+
+ remove(sequence, path);
+
+ return path;
+ }
+
+ /**
+ * Removes an item from a nested sequence.
+ *
+ * @param sequence
+ * The root sequence.
+ *
+ * @param path
+ * The path of the item to remove.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> void remove(Sequence<T> sequence, Sequence<Integer> path) {
+ if (sequence == null) {
+ throw new IllegalArgumentException("sequence is null.");
+ }
+
+ if (path == null) {
+ throw new IllegalArgumentException("path is null.");
+ }
+
+ int i = 0, n = path.getLength() - 1;
+ while (i < n) {
+ sequence = (Sequence<T>)sequence.get(path.get(i++));
+ }
+
+ sequence.remove(path.get(i), 1);
+ }
+
+ /**
+ * Retrieves an item from a nested sequence.
+ *
+ * @param sequence
+ * The root sequence.
+ *
+ * @param path
+ * The path of the item to retrieve.
+ *
+ * @return
+ * The item at the given path, or <tt>null</tt> if the path is empty.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T get(Sequence<T> sequence, Sequence<Integer> path) {
+ if (sequence == null) {
+ throw new IllegalArgumentException("sequence is null.");
+ }
+
+ if (path == null) {
+ throw new IllegalArgumentException("path is null.");
+ }
+
+ int i = 0, n = path.getLength() - 1;
+ while (i < n) {
+ sequence = (Sequence<T>)sequence.get(path.get(i++));
+ }
+
+ return sequence.get(path.get(i));
+ }
+
+ /**
+ * Returns the path to an item in a nested sequence.
+ *
+ * @param sequence
+ * The root sequence.
+ *
+ * @param item
+ * The item to locate.
+ *
+ * @return
+ * The path of first occurrence of the item if it exists in the
+ * sequence; <tt>null</tt>, otherwise.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> Sequence<Integer> pathOf(Sequence<T> sequence, T item) {
+ if (sequence == null) {
+ throw new IllegalArgumentException("sequence is null.");
+ }
+
+ if (item == null) {
+ throw new IllegalArgumentException("item is null.");
+ }
+
+ Sequence<Integer> path = null;
+
+ for (int i = 0, n = sequence.getLength(); i < n && path == null; i++) {
+ T t = sequence.get(i);
+
+ if (t.equals(item)) {
+ path = new ArrayList<Integer>();
+ path.add(i);
+ } else {
+ if (t instanceof Sequence) {
+ path = pathOf((Sequence<T>)t, item);
+
+ if (path != null) {
+ path.insert(0, i);
+ }
+ }
+ }
+ }
+
+ return path;
+ }
+
+ /**
+ * Returns the rank of an item in a nested sequence. An item's rank
+ * is defined as its position in a depth-first traversal of the tree.
+ *
+ * @param sequence
+ * The root sequence.
+ *
+ * @param item
+ * The item to locate.
+ *
+ * @return
+ * The rank of the first occurrence of the item if it exists in the
+ * sequence, or <tt>-(<i>descendant count</i> + 1)</tt> if the item is not
+ * found.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> int rankOf(Sequence<T> sequence, T item) {
+ if (sequence == null) {
+ throw new IllegalArgumentException("sequence is null.");
+ }
+
+ if (item == null) {
+ throw new IllegalArgumentException("item is null.");
+ }
+
+ int rank = -1;
+
+ for (int i = 0, n = sequence.getLength(); i < n && rank < 0; i++) {
+ rank--;
+
+ T t = sequence.get(i);
+
+ if (t.equals(item)) {
+ // The item was found in this sequence
+ rank = -(rank + 1);
+ } else {
+ if (t instanceof Sequence) {
+ int r = rankOf((Sequence<T>)t, item);
+
+ if (r < 0) {
+ // The item is not a descendant of this sequence
+ rank += (r + 1);
+ } else {
+ // The item was found in a nested sequence
+ rank = -(rank + 1) + r;
+ }
+ }
+ }
+ }
+
+ return rank;
+ }
+ }
+
+ /**
+ * Contains utility methods for searching sequences.
+ *
+ * @author gbrown, tvolkert
+ */
+ public static class Search {
+ /**
+ * Performs a binary search of a sequence for the given comparable item.
+ * See {@link #binarySearch(Sequence, Object, Comparator)}.
+ */
+ public static <T extends Comparable<? super T>> int binarySearch(Sequence<T> sequence, T item) {
+ Comparator<T> comparator = new Comparator<T>() {
+ public int compare(T t1, T t2) {
+ return t1.compareTo(t2);
+ }
+ };
+
+ return binarySearch(sequence, item, comparator);
+ }
+
+ /**
+ * Performs a binary search of a sequence for the given item.
+ *
+ * @param sequence
+ * The sequence to search. If the sequence is not sorted, the behavior
+ * is undefined.
+ *
+ * @param item
+ * The item to search for.
+ *
+ * @param comparator
+ * Comparator that determines element order.
+ *
+ * @return
+ * The index of <tt>item</tt>, if it is contained in the sequence;
+ * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The insertion
+ * point is defined as the point at which the item would be inserted
+ * into the sequence: the index of the first element greater than
+ * <tt>item</tt>, or <tt>sequence.getLength()</tt>, if all items in the
+ * sequence are less than <tt>item</tt>. Note that this guarantees that
+ * the return value will be greater than 0 if and only if the item is
+ * found.
+ * <p>
+ * If the sequence contains multiple elements equal to the specified
+ * item, there is no guarantee which one will be found.
+ */
+ public static <T> int binarySearch(Sequence<T> sequence, T item,
+ Comparator<T> comparator) {
+ int low = 0;
+ int high = sequence.getLength() - 1;
+
+ while (low <= high) {
+ int mid = (low + high) >> 1;
+ T midVal = sequence.get(mid);
+ int cmp = comparator.compare(midVal, item);
+
+ if (cmp < 0) {
+ low = mid + 1;
+ }
+ else if (cmp > 0) {
+ high = mid - 1;
+ }
+ else {
+ // Item found
+ return mid;
+ }
+ }
+
+ // Item not found
+ return -(low + 1);
+ }
+
+ /**
+ * Performs a linear search of a sequence for the given comparable item.
+ * See {@link #linearSearch(Sequence, Object, Comparator)}.
+ */
+ public static <T extends Comparable<? super T>> int linearSearch(Sequence<T> sequence, T item) {
+ Comparator<T> comparator = new Comparator<T>() {
+ public int compare(T t1, T t2) {
+ return t1.compareTo(t2);
+ }
+ };
+
+ return linearSearch(sequence, item, comparator);
+ }
+
+ /**
+ * Performs a linear search of a sequence for the given item.
+ *
+ * @param sequence
+ * The sequence to search.
+ *
+ * @param item
+ * The item to search for.
+ *
+ * @param comparator
+ * Comparator that will be used to determine logical equality.
+ *
+ * @return
+ * The index of <tt>item</tt>, if it is contained in the sequence;
+ * otherwise, <tt>-1</tt>.
+ * <p>
+ * If the sequence contains multiple elements equal to the specified
+ * item, this will return the first occurrence.
+ */
+ public static <T> int linearSearch(Sequence<T> sequence, T item,
+ Comparator<T> comparator) {
+ int index = -1;
+
+ for (int i = 0, n = sequence.getLength(); i < n; i++) {
+ T current = sequence.get(i);
+
+ if (comparator.compare(current, item) == 0) {
+ // Item found
+ index = i;
+ break;
+ }
+ }
+
+ return index;
+ }
+ }
+
+ /**
+ * Contains utility methods for sorting sequences.
+ *
+ * @author gbrown
+ */
+ public static class Sort {
+ /**
+ * Performs a quicksort on the sequence for the given comparable type.
+ * See {@link #quickSort(Sequence, Comparator)}.
+ */
+ public static <T extends Comparable<? super T>> void quickSort(Sequence<T> sequence) {
+ Comparator<T> comparator = new Comparator<T>() {
+ public int compare(T t1, T t2) {
+ return t1.compareTo(t2);
+ }
+ };
+
+ quickSort(sequence, comparator);
+ }
+
+ /**
+ * Performs a quicksort on the sequence.
+ *
+ * @param sequence
+ * The sequence to sort.
+ *
+ * @param comparator
+ * Comparator that determines element order.
+ */
+ public static <T> void quickSort(Sequence<T> sequence, Comparator<T> comparator) {
+ // TODO Implement internally rather than copying to java.util.ArrayList
+
+ java.util.ArrayList<T> arrayList = new java.util.ArrayList<T>(sequence.getLength());
+
+ for (int i = 0, n = sequence.getLength(); i < n; i++) {
+ arrayList.add(sequence.get(i));
+ }
+
+ java.util.Collections.sort(arrayList, comparator);
+
+ for (int i = 0, n = arrayList.size(); i < n; i++) {
+ sequence.update(i, arrayList.get(i));
+ }
+ }
+ }
+
+ /**
+ * Adds an item to the sequence.
+ *
+ * @param item
+ * The item to be added to the sequence.
+ *
+ * @return
+ * The index at which the item was added.
+ */
+ public int add(T item);
+
+ /**
+ * Inserts an item into the sequence at a specific index.
+ *
+ * @param item
+ * The item to be added to the sequence.
+ *
+ * @param index
+ * The index at which the item should be inserted. Must be a value between
+ * <tt>0</tt> and <tt>getLength()</tt>.
+ */
+ public void insert(T item, int index);
+
+ /**
+ * Updates the item at the given index.
+ *
+ * @param index
+ * The index of the item to update.
+ *
+ * @param item
+ * The item that will replace any existing value at the given index.
+ *
+ * @return
+ * The item that was previously stored at the given index.
+ */
+ public T update(int index, T item);
+
+ /**
+ * Removes the first occurrence of the given item from the sequence.
+ *
+ * @param item
+ * The item to remove.
+ *
+ * @return
+ * The index of the item that was removed, or <tt>-1</tt> if the item
+ * could not be found.
+ *
+ * @see #remove(int, int)
+ */
+ public int remove(T item);
+
+ /**
+ * Removes one or more items from the sequence.
+ *
+ * @param index
+ * The starting index to remove.
+ *
+ * @param count
+ * The number of items to remove, beginning with <tt>index</tt>.
+ *
+ * @return
+ * A sequence containing the items that were removed.
+ */
+ public Sequence<T> remove(int index, int count);
+
+ /**
+ * Retrieves the item at the given index.
+ *
+ * @param index
+ * The index of the item to retrieve.
+ */
+ public T get(int index);
+
+ /**
+ * Returns the index of an item in the sequence.
+ *
+ * @param item
+ * The item to locate.
+ *
+ * @return
+ * The index of first occurrence of the item if it exists in the sequence;
+ * <tt>-1</tt>, otherwise.
+ */
+ public int indexOf(T item);
+
+ /**
+ * Returns the length of the sequence.
+ *
+ * @return
+ * The number of items in the sequence.
+ */
+ public int getLength();
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/Set.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/Set.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/Set.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/Set.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+import pivot.util.ListenerList;
+
+/**
+ * Collection interface representing a group of unique elements.
+ *
+ * @author gbrown
+ */
+public interface Set<E> extends Group<E>, Collection<E> {
+ /**
+ * Set listener list implementation.
+ *
+ * @author gbrown
+ */
+ public static class SetListenerList<E>
+ extends ListenerList<SetListener<E>> implements SetListener<E> {
+ public void elementAdded(Set<E> set, E element) {
+ for (SetListener<E> listener : this) {
+ listener.elementAdded(set, element);
+ }
+ }
+
+ public void elementRemoved(Set<E> set, E element) {
+ for (SetListener<E> listener : this) {
+ listener.elementRemoved(set, element);
+ }
+ }
+
+ public void setCleared(Set<E> set) {
+ for (SetListener<E> listener : this) {
+ listener.setCleared(set);
+ }
+ }
+
+ public void comparatorChanged(Set<E> set, Comparator<E> previousComparator) {
+ for (SetListener<E> listener : this) {
+ listener.comparatorChanged(set, previousComparator);
+ }
+ }
+ }
+
+ /**
+ * @see SetListener#elementAdded(Set, Object)
+ */
+ public void add(E element);
+
+ /**
+ * @see SetListener#elementRemoved(Set, Object)
+ */
+ public void remove(E element);
+
+ /**
+ * @see SetListener#setCleared(Set)
+ */
+ public void clear();
+
+ /**
+ * @see SetListener#setCleared(Set)
+ */
+ public void setComparator(Comparator<E> comparator);
+
+ /**
+ * Returns the set listener collection.
+ */
+ public ListenerList<SetListener<E>> getSetListeners();
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/SetListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/SetListener.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/SetListener.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/SetListener.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+import java.util.Comparator;
+
+/**
+ * Set listener interface.
+ *
+ * @author gbrown
+ */
+public interface SetListener<E> {
+ /**
+ * Called when an element is added to a set.
+ *
+ * @param set
+ * The source of the set event.
+ *
+ * @param element
+ * The element that was added to the set.
+ */
+ public void elementAdded(Set<E> set, E element);
+
+ /**
+ * Called when an element is removed from the set.
+ *
+ * @param set
+ * The source of the set event.
+ *
+ * @param element
+ * The element that was removed from the set.
+ */
+ public void elementRemoved(Set<E> set, E element);
+
+ /**
+ * Called when set data has been reset.
+ *
+ * @param set
+ * The source of the set event.
+ */
+ public void setCleared(Set<E> set);
+
+ /**
+ * Called when a set's comparator has changed.
+ *
+ * @param set
+ * The source of the event.
+ *
+ * @param previousComparator
+ * The previous comparator value.
+ */
+ public void comparatorChanged(Set<E> set, Comparator<E> previousComparator);
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/Stack.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/Stack.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/Stack.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/Stack.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections;
+
+/**
+ * Interface representing a last-in, first-out (LIFO) stack when unsorted, and
+ * a priority stack when sorted.
+ *
+ * @author gbrown
+ */
+public interface Stack<T> extends List<T> {
+ /**
+ * "Pushes" an item onto the stack. If the stack is unsorted, the item is
+ * added at the top of the stack (<tt>getLength()</tt>). Otherwise, it is
+ * inserted at the appropriate index.
+ *
+ * @param item
+ * The item to push onto the stack.
+ */
+ public void push(T item);
+
+ /**
+ * Removes the top item from the stack and returns it.
+ *
+ * @throws IllegalStateException
+ * If the stack contains no items.
+ */
+ public T pop();
+
+ /**
+ * Returns the item on top of the stack without removing it from the stack.
+ * Returns null if the stack contains no items. Will also return null if the
+ * top item in the stack is null. <tt>getLength()</tt> can be used to
+ * distinguish between these two cases.
+ */
+ public T peek();
+
+ /**
+ * Replaces the item on top of the stack.
+ *
+ * @throws IllegalStateException
+ * If the stack contains no items.
+ */
+ public T poke(T item);
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/ListAdapter.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/ListAdapter.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/ListAdapter.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/ListAdapter.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections.adapter;
+
+import java.util.Comparator;
+import java.util.Iterator;
+import pivot.collections.List;
+import pivot.collections.ListListener;
+import pivot.collections.Sequence;
+import pivot.util.ImmutableIterator;
+import pivot.util.ListenerList;
+
+/**
+ * Implementation of the {@link List} interface that is backed by an
+ * instance of <tt>java.util.List</tt>.
+ */
+public final class ListAdapter<T> implements List<T> {
+ private java.util.List<T> list = null;
+ private ListListenerList<T> listListeners = new ListListenerList<T>();
+
+ public ListAdapter(java.util.List<T> list) {
+ if (list == null) {
+ throw new IllegalArgumentException("list is null.");
+ }
+
+ this.list = list;
+ }
+
+ public int add(T item) {
+ int index = list.size();
+ list.add(index, item);
+ listListeners.itemInserted(this, index);
+
+ return index;
+ }
+
+ public void insert(T item, int index) {
+ list.add(index, item);
+ listListeners.itemInserted(this, index);
+ }
+
+ public T update(int index, T item) {
+ T previousItem = list.set(index, item);
+ listListeners.itemUpdated(this, index, previousItem);
+
+ return previousItem;
+ }
+
+ /**
+ * NOTE This method is not supported because it cannot be efficiently
+ * implemented for all list types.
+ */
+ public int remove(T item) {
+ throw new UnsupportedOperationException();
+ }
+
+ @SuppressWarnings("unchecked")
+ public Sequence<T> remove(int index, int count) {
+ java.util.List<T> removedList = null;
+ try {
+ removedList = (java.util.List<T>)list.getClass().newInstance();
+ } catch(IllegalAccessException exception) {
+ } catch(InstantiationException exception) {
+ }
+
+ for (int i = count - 1; i >= 0; i--) {
+ removedList.add(0, list.remove(index + i));
+ }
+
+ // Fire event
+ List<T> removed = new ListAdapter<T>(removedList);
+ listListeners.itemsRemoved(this, index, removed);
+
+ return removed;
+ }
+
+ public void clear() {
+ list.clear();
+ listListeners.itemsRemoved(this, 0, null);
+ }
+
+ public T get(int index) {
+ return list.get(index);
+ }
+
+ public int indexOf(T item) {
+ return list.indexOf(item);
+ }
+
+ public int getLength() {
+ return list.size();
+ }
+
+ public Comparator<T> getComparator() {
+ return null;
+ }
+
+ /**
+ * NOTE This method is not supported because it cannot be efficiently
+ * implemented for all list types.
+ */
+ public void setComparator(Comparator<T> comparator) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Iterator<T> iterator() {
+ return new ImmutableIterator<T>(list.iterator());
+ }
+
+ public ListenerList<ListListener<T>> getListListeners() {
+ return listListeners;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/MapAdapter.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/MapAdapter.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/MapAdapter.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/MapAdapter.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections.adapter;
+
+import java.util.Comparator;
+import java.util.Iterator;
+
+import pivot.collections.Map;
+import pivot.collections.MapListener;
+import pivot.util.ImmutableIterator;
+import pivot.util.ListenerList;
+
+/**
+ * Implementation of the {@link Map} interface that is backed by an
+ * instance of <tt>java.util.Map</tt>.
+ */
+public class MapAdapter<K, V> implements Map<K, V> {
+ private java.util.Map<K, V> map = null;
+ private MapListenerList<K, V> mapListeners = new MapListenerList<K, V>();
+
+ public MapAdapter(java.util.Map<K, V> map) {
+ if (map == null) {
+ throw new IllegalArgumentException("map is null.");
+ }
+
+ this.map = map;
+ }
+
+ public V get(K key) {
+ return map.get(key);
+ }
+
+ public V put(K key, V value) {
+ boolean update = map.containsKey(key);
+ V previousValue = map.put(key, value);
+
+ if (update) {
+ mapListeners.valueUpdated(this, key, previousValue);
+ }
+ else {
+ mapListeners.valueAdded(this, key);
+ }
+
+ return previousValue;
+ }
+
+ public V remove(K key) {
+ V value = null;
+
+ if (map.containsKey(key)) {
+ value = map.remove(key);
+ mapListeners.valueRemoved(this, key, value);
+ }
+
+ return value;
+ }
+
+ public void clear() {
+ map.clear();
+ mapListeners.mapCleared(this);
+ }
+
+ public boolean containsKey(K key) {
+ return map.containsKey(key);
+ }
+
+
+ public boolean isEmpty() {
+ return map.isEmpty();
+ }
+
+ public Comparator<K> getComparator() {
+ return null;
+ }
+
+ /**
+ * NOTE This method is not supported because it cannot be efficiently
+ * implemented for all map types.
+ */
+ public void setComparator(Comparator<K> comparator) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Iterator<K> iterator() {
+ return new ImmutableIterator<K>(map.keySet().iterator());
+ }
+
+ public ListenerList<MapListener<K, V>> getMapListeners() {
+ return mapListeners;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/SetAdapter.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/SetAdapter.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/SetAdapter.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/SetAdapter.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections.adapter;
+
+import java.util.Comparator;
+import java.util.Iterator;
+
+import pivot.collections.Set;
+import pivot.collections.SetListener;
+import pivot.util.ImmutableIterator;
+import pivot.util.ListenerList;
+
+/**
+ * Implementation of the {@link Set} interface that is backed by an
+ * instance of <tt>java.util.Set</tt>.
+ */
+public class SetAdapter<E> implements Set<E> {
+ private java.util.Set<E> set = null;
+ private SetListenerList<E> setListeners = new SetListenerList<E>();
+
+ public SetAdapter(java.util.Set<E> set) {
+ if (set == null) {
+ throw new IllegalArgumentException("set is null.");
+ }
+
+ this.set = set;
+ }
+
+ public void add(E element) {
+ if (!set.contains(element)) {
+ set.add(element);
+ setListeners.elementAdded(this, element);
+ }
+ }
+
+ public void remove(E element) {
+ if (set.contains(element)) {
+ set.remove(element);
+ setListeners.elementRemoved(this, element);
+ }
+ }
+
+ public void clear() {
+ set.clear();
+ setListeners.setCleared(this);
+ }
+
+ public boolean contains(E element) {
+ return set.contains(element);
+ }
+
+ public boolean isEmpty() {
+ return set.isEmpty();
+ }
+
+ public Comparator<E> getComparator() {
+ return null;
+ }
+
+ /**
+ * NOTE This method is not supported because it cannot be efficiently
+ * implemented for all set types.
+ */
+ public void setComparator(Comparator<E> comparator) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Iterator<E> iterator() {
+ return new ImmutableIterator<E>(set.iterator());
+ }
+
+ public ListenerList<SetListener<E>> getSetListeners() {
+ return setListeners;
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/package.html
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/package.html?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/package.html (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/adapter/package.html Mon Mar 16 16:16:40 2009
@@ -0,0 +1,6 @@
+<html>
+<head></head>
+<body>
+<p>Provides a set of collection implementations that are backed by java.util collections.</p>
+</body>
+</html>
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/concurrent/SynchronizedCollection.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/concurrent/SynchronizedCollection.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/concurrent/SynchronizedCollection.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/concurrent/SynchronizedCollection.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections.concurrent;
+
+import java.util.Comparator;
+import java.util.Iterator;
+
+import pivot.collections.Collection;
+import pivot.util.ImmutableIterator;
+
+/**
+ * Abstract base class for synchronized collection wrappers.
+ * <p>
+ * NOTE In order to guarantee thread-safe access, all access to the backing
+ * collection must be via the synchronized wrapper.
+ *
+ * @author gbrown
+ */
+public abstract class SynchronizedCollection<T> implements Collection<T> {
+ protected Collection<T> collection = null;
+
+ public SynchronizedCollection(Collection<T> collection) {
+ if (collection == null) {
+ throw new IllegalArgumentException("collection is null.");
+ }
+
+ this.collection = collection;
+ }
+
+ public synchronized Comparator<T> getComparator() {
+ return collection.getComparator();
+ }
+
+ public synchronized void setComparator(Comparator<T> comparator) {
+ collection.setComparator(comparator);
+ }
+
+ /**
+ * NOTE Callers must manually synchronize on the SynchronizedCollection
+ * instance to ensure thread safety during iteration.
+ */
+ public Iterator<T> iterator() {
+ return new ImmutableIterator<T>(collection.iterator());
+ }
+}
Added: incubator/pivot/tags/v1.0/core/src/pivot/collections/concurrent/SynchronizedList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/tags/v1.0/core/src/pivot/collections/concurrent/SynchronizedList.java?rev=754926&view=auto
==============================================================================
--- incubator/pivot/tags/v1.0/core/src/pivot/collections/concurrent/SynchronizedList.java (added)
+++ incubator/pivot/tags/v1.0/core/src/pivot/collections/concurrent/SynchronizedList.java Mon Mar 16 16:16:40 2009
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2008 VMware, Inc.
+ *
+ * Licensed 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 pivot.collections.concurrent;
+
+import java.util.Comparator;
+import pivot.collections.List;
+import pivot.collections.ListListener;
+import pivot.collections.Sequence;
+import pivot.util.ListenerList;
+import pivot.util.concurrent.SynchronizedListenerList;
+
+/**
+ * Synchronized implementation of the {@link List} interface.
+ *
+ * @author gbrown
+ */
+public class SynchronizedList<T> extends SynchronizedCollection<T>
+ implements List<T> {
+ /**
+ * Synchronized list listener list implementation. Proxies events fired
+ * by inner list to listeners of synchronized list.
+ *
+ * @author gbrown
+ */
+ private class SynchronizedListListenerList
+ extends SynchronizedListenerList<ListListener<T>>
+ implements ListListener<T> {
+ public synchronized void itemInserted(List<T> list, int index) {
+ for (ListListener<T> listener : this) {
+ listener.itemInserted(SynchronizedList.this, index);
+ }
+ }
+
+ public synchronized void itemsRemoved(List<T> list, int index, Sequence<T> items) {
+ for (ListListener<T> listener : this) {
+ listener.itemsRemoved(SynchronizedList.this, index, items);
+ }
+ }
+
+ public synchronized void itemUpdated(List<T> list, int index, T previousItem) {
+ for (ListListener<T> listener : this) {
+ listener.itemUpdated(SynchronizedList.this, index, previousItem);
+ }
+ }
+
+ public synchronized void comparatorChanged(List<T> list, Comparator<T> previousComparator) {
+ for (ListListener<T> listener : this) {
+ listener.comparatorChanged(SynchronizedList.this, previousComparator);
+ }
+ }
+ }
+
+ private SynchronizedListListenerList listListeners = new SynchronizedListListenerList();
+
+ public SynchronizedList(List<T> list) {
+ super(list);
+
+ list.getListListeners().add(listListeners);
+ }
+
+ public synchronized int add(T item) {
+ return ((List<T>)collection).add(item);
+ }
+
+ public synchronized void insert(T item, int index) {
+ ((List<T>)collection).insert(item, index);
+ }
+
+ public synchronized T update(int index, T item) {
+ return ((List<T>)collection).update(index, item);
+ }
+
+ public int remove (T item) {
+ return ((List<T>)collection).remove(item);
+ }
+
+ public synchronized Sequence<T> remove(int index, int count) {
+ return ((List<T>)collection).remove(index, count);
+ }
+
+ public synchronized void clear() {
+ ((List<T>)collection).clear();
+ }
+
+ public synchronized T get(int index) {
+ return ((List<T>)collection).get(index);
+ }
+
+ public synchronized int indexOf(T item) {
+ return ((List<T>)collection).indexOf(item);
+ }
+
+ public synchronized int getLength() {
+ return ((List<T>)collection).getLength();
+ }
+
+ public ListenerList<ListListener<T>> getListListeners() {
+ return listListeners;
+ }
+}