From b64181a4cf54eb4e0737f83a004c298996d578e5 Mon Sep 17 00:00:00 2001 From: Julian Hyde Date: Tue, 19 Apr 2011 03:53:54 +0000 Subject: [PATCH] Add Olap4jUtil.toMap(Properties) and .unmodifiableNamedList(NamedList). git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@452 c6a108a4-781c-0410-a6c6-c2d559e19af0 --- src/org/olap4j/impl/Olap4jUtil.java | 160 ++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/src/org/olap4j/impl/Olap4jUtil.java b/src/org/olap4j/impl/Olap4jUtil.java index 314e7be..e28c364 100644 --- a/src/org/olap4j/impl/Olap4jUtil.java +++ b/src/org/olap4j/impl/Olap4jUtil.java @@ -181,6 +181,20 @@ public static Map 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 toMap(final Properties properties) { + return new AbstractMap() { + public Set> entrySet() { + return cast(properties.entrySet()); + } + }; + } + /** * Returns an exception indicating that we didn't expect to find this value * here. @@ -437,6 +451,32 @@ public static NamedList emptyNamedList() { return (NamedList) 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 + * UnsupportedOperationException.

+ * + * 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. + * + *

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 NamedList unmodifiableNamedList( + final NamedList list) + { + return list instanceof RandomAccess + ? new UnmodifiableNamedRandomAccessList(list) + : new UnmodifiableNamedList(list); + } + /** * Equivalent to {@link java.util.EnumSet#of(Enum, Enum[])} on JDK 1.5 or * later. Otherwise, returns an ordinary set. @@ -585,6 +625,126 @@ private Object readResolve() { return EMPTY_NAMED_LIST; } } + + private static class UnmodifiableNamedList implements NamedList { + private final NamedList list; + + UnmodifiableNamedList(NamedList 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 iterator() { + return Collections.unmodifiableList(list).iterator(); + } + + public Object[] toArray() { + return list.toArray(); + } + + public 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 c) { + throw new UnsupportedOperationException(); + } + + public boolean addAll(int index, Collection 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 listIterator() { + return Collections.unmodifiableList(list).listIterator(); + } + + public ListIterator listIterator(int index) { + return Collections.unmodifiableList(list).listIterator(index); + } + + public List subList(int fromIndex, int toIndex) { + // TODO: NamedList.subList should return NamedList. + return Collections.unmodifiableList( + list.subList(fromIndex, toIndex)); + } + } + + private static class UnmodifiableNamedRandomAccessList + extends UnmodifiableNamedList + implements RandomAccess + { + UnmodifiableNamedRandomAccessList(NamedList list) { + super(list); + } + } } // End Olap4jUtil.java