faa.tg.util.adapter
Class Adapters

java.lang.Object
  extended by faa.tg.util.adapter.Adapters

public class Adapters
extends java.lang.Object

 This is a utility class for hanging utility methods based off of
 Adapter.
 
See the source: Adapters.java

Version:
$Id: Adapters.java,v 1.8 2009/01/26 15:22:32 lykensj Exp $
Author:
lykensj, Engility Oct 28, 2008 3:03:28 PM

Nested Class Summary
private static class Adapters.AdaptedCollection<F,T>
           
private static class Adapters.AdaptedEntry<K,V,L,W>
           
private static class Adapters.AdaptedEntryIterator<K,V,L,W>
           
private static class Adapters.AdaptedEntrySet<K,V,L,W>
           
private static class Adapters.AdaptedIterable<F,T>
           
private static class Adapters.AdaptedIterator<F,T>
          
private static class Adapters.ArbitraryIteratorIterable<C,V>
           
private static class Adapters.ArrayAdapter<T>
           
private static class Adapters.ArrayIterator<T>
           
private static class Adapters.ConvertedMap<K,V,W>
           
private static class Adapters.DequeReverserAdapter<F>
           
private static class Adapters.FilteredIterable<T>
           
private static class Adapters.FilteredIterator<T>
           
private static class Adapters.IdentityAdapter<V>
           
private static class Adapters.MapBackedAdapter<K,V>
           
private static class Adapters.MergedAdapter<F,T,P>
           
private static class Adapters.SetFilter<T>
           
private static class Adapters.UpcastingIterator<V>
           
 
Field Summary
static Adapter<java.lang.Enum<?>,java.lang.String> ENUM_NAME
           
 
Constructor Summary
Adapters()
           
 
Method Summary
static
<T> java.lang.Iterable<T>
arrayIterator(T[] array)
          Wraps an array with an iterable.
static
<T> java.lang.Iterable<T>
arrayIterator(T[] array, int start, int end)
          Wraps an array with an iterable.
static
<T> Filter<T>
containedWithin(java.util.Set<? super T> set)
          Creates a filter that accepts only the objects that are within the given set.
static
<F,T> java.util.Collection<T>
convertCollection(java.util.Collection<? extends F> source, Adapter<? super F,? extends T> adapter)
          Creates a view of the given Collection via the given adapter.
static
<K,V,L> java.util.Set<java.util.Map.Entry<L,V>>
convertEntrySet(java.util.Map<K,V> map, Adapter<? super K,? extends L> keyAdapter)
          Batch converts a map into an entry Set.
static
<K,V,L,W> java.util.Set<java.util.Map.Entry<L,W>>
convertEntrySet(java.util.Map<K,V> map, Adapter<? super K,? extends L> keyAdapter, Adapter<? super V,? extends W> valueAdapter)
          Batch converts a map into an entry Set.
static
<F,T> java.lang.Iterable<T>
convertIterable(java.lang.Iterable<? extends F> source, Adapter<? super F,? extends T> adapter)
          Creates a view of the given Iterable via the given adapter.
static
<K,V> void
fillMap(java.util.Map<? super K,V> map, java.lang.Iterable<? extends V> iterable, Adapter<? super V,? extends K> adapter)
          Converts all objects inside an iterable with the adapter provided and adds them to the given map.
static
<T> java.lang.Iterable<T>
filter(java.lang.Iterable<? extends T> iter, Filter<? super T> filter)
          Filters an iterable by the filter provided, where the iterable contains only the objects that are accepted by the filter.
static
<V> Adapter<V,V>
identityAdapter(java.lang.Class<V> clazz)
          Creates an adapter that just returns whatever is given to it.
static
<K,V,W> java.util.Map<K,W>
interpretedMap(java.util.Map<K,? extends V> map, Adapter<? super V,? extends W> adapter)
          Creates an interpreted map by using the adapter and backing map provided.
static
<T> Filter<T>
invert(Filter<? super T> filter)
          Uses a filter to create another that accepts only the objects that are rejected by the source filter.
static
<C,V> java.lang.Iterable<V>
iterableOver(C source, Adapter<? super C,java.util.Iterator<V>> adapter)
          Creates an iterable from a source and an adapter.
static
<F,T,P> Adapter<F,P>
merge(Adapter<? super F,? extends T> first, Adapter<? super T,? extends P> second)
          Merges two adapters transitively to make another.
static
<T> Filter<T>
notWithin(java.util.Set<? super T> set)
          Creates a filter that rejects only the objects that are within the given set.
static
<V> java.lang.Iterable<V>
reversedDeque(java.util.Deque<? extends V> source)
          Reverses a deque.
static
<T> void
subtract(java.lang.Iterable<? extends T> modified, Filter<? super T> filter)
          Removes all elements of the iterable that are accepted by the filter.
static
<T> void
subtract(java.lang.Iterable<? extends T> modified, java.util.Set<? super T> set)
          Removes all elements of the iterable that are within the set provided.
static
<F,T> java.lang.Iterable<T>
synchronizedConvertArrayRange(java.util.List<F> list, Adapter<? super F,? extends T> adapter, int i, int f)
          Creates an range-bounded Iterable from a list in a thread-safe manner.
static
<K,V> Adapter<K,V>
toAdapter(java.util.Map<? super K,? extends V> map)
          Makes an Adapter from a Map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENUM_NAME

public static final Adapter<java.lang.Enum<?>,java.lang.String> ENUM_NAME
Constructor Detail

Adapters

public Adapters()
Method Detail

convertCollection

public static final <F,T> java.util.Collection<T> convertCollection(java.util.Collection<? extends F> source,
                                                                    Adapter<? super F,? extends T> adapter)
Creates a view of the given Collection via the given adapter. More specifically, it creates a dynamically calculated Collection given by calling adapter.convert on the elements inside the source collection's iterator. Methods such as contains(Object) on the returned Collection work in O(n) time regardless of potentially better methods. As the new collection is backed by the source collection, changes in the source collection are reflected in the new collection.The opposite is also true; remove(Object) and similar methods on the new Collection cause the corresponding elements in the source collection to be removed as well. However, functions that add to the new collection are not permitted as there is no way to add an appropriate object back into the original collection. To create a snapshot view that is not backed by the original map, consider using the constructors of ArrayList or similar classes in java.util that take an Iterable. As iterator for the collection is dynamically created, there is only a constant-time penalty for creating the Collection and Iterator wrapper.

Type Parameters:
F - the existing type of Collection
T - the type of Collection to convert to.
Parameters:
source - the collection to convert.
adapter - how to convert Objects of type F into Objects of type T
Returns:
a new Collection of type T backed by the original Collection.

convertIterable

public static final <F,T> java.lang.Iterable<T> convertIterable(java.lang.Iterable<? extends F> source,
                                                                Adapter<? super F,? extends T> adapter)
Creates a view of the given Iterable via the given adapter. The Iterable is a dynamically generated factory for Iterators using the given adapter. As the new Iterable is backed by the source Iterable, changes in the source are reflected in the returned Iterable.The opposite is also true; remove() causes the corresponding elements in the source Iterable to be removed as well.

Type Parameters:
F - the existing type of Iterable
T - the type of Collection to convert to.
Parameters:
source - the collection to convert.
adapter - how to convert Objects of type F into Objects of type T
Returns:
a new Collection of type T backed by the original Collection.

fillMap

public static final <K,V> void fillMap(java.util.Map<? super K,V> map,
                                       java.lang.Iterable<? extends V> iterable,
                                       Adapter<? super V,? extends K> adapter)
Converts all objects inside an iterable with the adapter provided and adds them to the given map.

Type Parameters:
K - the mediating key type
V - the value type of the map.
Parameters:
map - the map to add to.
iterable - the iterable to add from.
adapter - the adapter to convert with.

reversedDeque

public static <V> java.lang.Iterable<V> reversedDeque(java.util.Deque<? extends V> source)
Reverses a deque.

Type Parameters:
V -
Parameters:
source - the deque to reverse.
Returns:
the reversed deque as an iterable.

iterableOver

public static <C,V> java.lang.Iterable<V> iterableOver(C source,
                                                       Adapter<? super C,java.util.Iterator<V>> adapter)
Creates an iterable from a source and an adapter.

Type Parameters:
C -
V -
Parameters:
source -
adapter -
Returns:

arrayIterator

public static <T> java.lang.Iterable<T> arrayIterator(T[] array)
Wraps an array with an iterable.

Type Parameters:
T -
Parameters:
array -
Returns:

arrayIterator

public static <T> java.lang.Iterable<T> arrayIterator(T[] array,
                                                      int start,
                                                      int end)
Wraps an array with an iterable.

Type Parameters:
T -
Parameters:
array -
start - the starting index to iterate over.
end - the ending condition value. Equivalent to the end index - 1
Returns:

synchronizedConvertArrayRange

public static <F,T> java.lang.Iterable<T> synchronizedConvertArrayRange(java.util.List<F> list,
                                                                        Adapter<? super F,? extends T> adapter,
                                                                        int i,
                                                                        int f)
Creates an range-bounded Iterable from a list in a thread-safe manner. Subsequent changes to the source list will not change the Iterable. For concurrency purposes, a lock on the list is acquired and then java.util.List#toArray() is called and then the lock is released. This will only be "fast" if the underlying structure of the list is an array and the toArray() method takes advantage of this fact; calling this method on java.util.LinkedList, for example, is not recommended if speed is a concern. The adapter is then lazily called to produce the new iterable.

Type Parameters:
F - the type of the list
T - what to convert to.
Parameters:
list - the source list of to convert.
adapter - how to convert from to
i - the starting index to iterate over.
f - the ending condition value. Equivalent to the end index - 1
Returns:
An Iterable of based on the adapter and range provided.

merge

public static final <F,T,P> Adapter<F,P> merge(Adapter<? super F,? extends T> first,
                                               Adapter<? super T,? extends P> second)
Merges two adapters transitively to make another. So the first adapter which takes Objects of type F returns type T is combined with the second that takes type T and returns type P to make a new adapter that takes type F and returns type P. Mathematically, two functions h:F->T and g:T->P are nested and the function r:F->P is created, where r(x) = g(h(x)). If either adapter returns null for a given input, the merged adapter will return null.

Type Parameters:
F - the type the first adapter converts into type T
T - the type the first adapter converts to and what the second adapter converts into type P
P - the type the second adapter converts objects of type T to.
Parameters:
first - the adapter equivalent to the function h.
second - the adapter equivalent to the function g.
Returns:
a new Adapter that takes Objects of type F and returns Objects of type P.

toAdapter

public static final <K,V> Adapter<K,V> toAdapter(java.util.Map<? super K,? extends V> map)
Makes an Adapter from a Map. For K that are not in the given map, the convert method will return null. The Adapter is backed by the map, so any changes in the source map will be reflected in the adapter.

Type Parameters:
K - the key type of the map and what to convert from in the adapter.
V - the value type of the map and what to convert to in the adapter.
Parameters:
map - the map to use as a backing for the returned adapter.
Returns:
an adapter based on the map provided.

convertEntrySet

public static final <K,V,L,W> java.util.Set<java.util.Map.Entry<L,W>> convertEntrySet(java.util.Map<K,V> map,
                                                                                      Adapter<? super K,? extends L> keyAdapter,
                                                                                      Adapter<? super V,? extends W> valueAdapter)
Batch converts a map into an entry Set.

See Also:
Map.Entry

convertEntrySet

public static final <K,V,L> java.util.Set<java.util.Map.Entry<L,V>> convertEntrySet(java.util.Map<K,V> map,
                                                                                    Adapter<? super K,? extends L> keyAdapter)
Batch converts a map into an entry Set.

See Also:
Map.Entry

identityAdapter

public static final <V> Adapter<V,V> identityAdapter(java.lang.Class<V> clazz)
Creates an adapter that just returns whatever is given to it.


filter

public static final <T> java.lang.Iterable<T> filter(java.lang.Iterable<? extends T> iter,
                                                     Filter<? super T> filter)
Filters an iterable by the filter provided, where the iterable contains only the objects that are accepted by the filter. The resulting iterable is backed by the source iterable, so any changes in the source are reflected in the result.

Type Parameters:
T - mediating type between the filter and the iterable.
Parameters:
iter - the source iteratable to filter.
filter - determines if an element should be added.
Returns:
a new iterator with the elements that are accepted by the filter.

containedWithin

public static final <T> Filter<T> containedWithin(java.util.Set<? super T> set)
Creates a filter that accepts only the objects that are within the given set.

Type Parameters:
T -
Parameters:
set - which containment determines acceptance
Returns:
a filter backed by the set provided.

notWithin

public static final <T> Filter<T> notWithin(java.util.Set<? super T> set)
Creates a filter that rejects only the objects that are within the given set.

Type Parameters:
T -
Parameters:
set - which containment determines acceptance
Returns:
a filter backed by the set provided.

subtract

public static final <T> void subtract(java.lang.Iterable<? extends T> modified,
                                      java.util.Set<? super T> set)
Removes all elements of the iterable that are within the set provided.

Type Parameters:
T - mediating type between the set and the iterable.
Parameters:
modified - the iterable to remove from.
set - that consists of elements that should be removed if they are within the modified set.

subtract

public static final <T> void subtract(java.lang.Iterable<? extends T> modified,
                                      Filter<? super T> filter)
Removes all elements of the iterable that are accepted by the filter.

Type Parameters:
T - mediating type between the filter and the iterable.
Parameters:
modified - the iterable to remove from
filter - the filter that accepts the things that should be removed.

invert

public static final <T> Filter<T> invert(Filter<? super T> filter)
Uses a filter to create another that accepts only the objects that are rejected by the source filter.

Type Parameters:
T -
Parameters:
filter - the backing filter.
Returns:
the inverted filter.

interpretedMap

public static <K,V,W> java.util.Map<K,W> interpretedMap(java.util.Map<K,? extends V> map,
                                                        Adapter<? super V,? extends W> adapter)
Creates an interpreted map by using the adapter and backing map provided. The values of the returned map are that of the values of the returned map with the adapter applied to them. The adapter is applied dynamically and so any changes in the backing map are reflected in the interpreted map. Any attempt to add to the interpreted map will result in an UnsupportedOperationException as there is no way to guarantee that the adapter can be reversed.

Type Parameters:
K - the key type of the maps
V - the intersecting type of the values of the backing map. That is, a type that accepted by both the map and the adapter provided.
W - the new value type of the interpreted map.
Parameters:
map - the source map
adapter - the adapter to convert s to s
Returns:
a of map the new type as a view of the the original map.