Skip to content

Commit

Permalink
Add Olap4jUtil.toMap(Properties) and .unmodifiableNamedList(NamedList).
Browse files Browse the repository at this point in the history
git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@452 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
julianhyde committed Apr 19, 2011
1 parent 6e9b2b6 commit b64181a
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions src/org/olap4j/impl/Olap4jUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ public static <K, V> Map<K, V> mapOf(K key, V value, Object... keyValues)
return map;
}

/**
* Converts a Properties object to a Map with String keys and values.
*
* @param properties Properties
* @return Map backed by the given Properties object
*/
public static Map<String, String> toMap(final Properties properties) {
return new AbstractMap<String, String>() {
public Set<Entry<String, String>> entrySet() {
return cast(properties.entrySet());
}
};
}

/**
* Returns an exception indicating that we didn't expect to find this value
* here.
Expand Down Expand Up @@ -437,6 +451,32 @@ public static <T> NamedList<T> emptyNamedList() {
return (NamedList<T>) EMPTY_NAMED_LIST;
}

/**
* Returns an unmodifiable view of the specified list. This method allows
* modules to provide users with "read-only" access to internal
* lists. Query operations on the returned list "read through" to the
* specified list, and attempts to modify the returned list, whether
* direct or via its iterator, result in an
* <tt>UnsupportedOperationException</tt>.<p>
*
* The returned list will be serializable if the specified list
* is serializable. Similarly, the returned list will implement
* {@link RandomAccess} if the specified list does.
*
* <p>The equivalent of
* {@link java.util.Collections#unmodifiableList(java.util.List)}.
*
* @param list the list for which an unmodifiable view is to be returned.
* @return an unmodifiable view of the specified list.
*/
public static <T> NamedList<T> unmodifiableNamedList(
final NamedList<? extends T> list)
{
return list instanceof RandomAccess
? new UnmodifiableNamedRandomAccessList<T>(list)
: new UnmodifiableNamedList<T>(list);
}

/**
* Equivalent to {@link java.util.EnumSet#of(Enum, Enum[])} on JDK 1.5 or
* later. Otherwise, returns an ordinary set.
Expand Down Expand Up @@ -585,6 +625,126 @@ private Object readResolve() {
return EMPTY_NAMED_LIST;
}
}

private static class UnmodifiableNamedList<T> implements NamedList<T> {
private final NamedList<? extends T> list;

UnmodifiableNamedList(NamedList<? extends T> list) {
this.list = list;
}

public T get(String s) {
return list.get(s);
}

public int indexOfName(String s) {
return list.indexOfName(s);
}

public int size() {
return list.size();
}

public boolean isEmpty() {
return list.isEmpty();
}

public boolean contains(Object o) {
return list.contains(o);
}

public Iterator<T> iterator() {
return Collections.unmodifiableList(list).iterator();
}

public Object[] toArray() {
return list.toArray();
}

public <T2> T2[] toArray(T2[] a) {
//noinspection SuspiciousToArrayCall
return list.toArray(a);
}

public boolean add(T t) {
throw new UnsupportedOperationException();
}

public boolean remove(Object o) {
throw new UnsupportedOperationException();
}

public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException();
}

public boolean addAll(Collection<? extends T> c) {
throw new UnsupportedOperationException();
}

public boolean addAll(int index, Collection<? extends T> c) {
throw new UnsupportedOperationException();
}

public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}

public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}

public void clear() {
throw new UnsupportedOperationException();
}

public T get(int index) {
return list.get(index);
}

public T set(int index, T element) {
throw new UnsupportedOperationException();
}

public void add(int index, T element) {
throw new UnsupportedOperationException();
}

public T remove(int index) {
throw new UnsupportedOperationException();
}

public int indexOf(Object o) {
return list.indexOf(0);
}

public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
}

public ListIterator<T> listIterator() {
return Collections.unmodifiableList(list).listIterator();
}

public ListIterator<T> listIterator(int index) {
return Collections.unmodifiableList(list).listIterator(index);
}

public List<T> subList(int fromIndex, int toIndex) {
// TODO: NamedList.subList should return NamedList.
return Collections.unmodifiableList(
list.subList(fromIndex, toIndex));
}
}

private static class UnmodifiableNamedRandomAccessList<T>
extends UnmodifiableNamedList<T>
implements RandomAccess
{
UnmodifiableNamedRandomAccessList(NamedList<? extends T> list) {
super(list);
}
}
}

// End Olap4jUtil.java

0 comments on commit b64181a

Please sign in to comment.