Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e11a1f

Browse files
committedFeb 24, 2012
Add methods .elementName and .asMap() to NamedList.
git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@509 c6a108a4-781c-0410-a6c6-c2d559e19af0
1 parent 75f5545 commit 5e11a1f

9 files changed

+135
-25
lines changed
 

‎src/org/olap4j/driver/xmla/DeferredNamedListImpl.java

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.olap4j.metadata.NamedList;
2626

2727
import java.util.AbstractList;
28+
import java.util.Map;
2829

2930
/**
3031
* Named list which instantiates itself on first use.
@@ -116,6 +117,14 @@ public int indexOfName(String name) {
116117
return getList().indexOfName(name);
117118
}
118119

120+
public String elementName(Object element) {
121+
return getList().elementName(element);
122+
}
123+
124+
public Map<String, T> asMap() {
125+
return getList().asMap();
126+
}
127+
119128
protected void populateList(NamedList<T> list) throws OlapException {
120129
context.olap4jConnection.populateList(
121130
list, context, metadataRequest, handler, restrictions);

‎src/org/olap4j/driver/xmla/XmlaOlap4jCellSetMetaData.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ class XmlaOlap4jCellSetMetaData implements CellSetMetaData {
4040
final XmlaOlap4jCube cube;
4141
private final NamedList<CellSetAxisMetaData> axisMetaDataList =
4242
new ArrayNamedListImpl<CellSetAxisMetaData>() {
43-
protected String getName(CellSetAxisMetaData axisMetaData) {
44-
return axisMetaData.getAxisOrdinal().name();
43+
public String elementName(Object axisMetaData) {
44+
return ((CellSetAxisMetaData) axisMetaData).getAxisOrdinal()
45+
.name();
4546
}
4647
};
4748
private final XmlaOlap4jCellSetAxisMetaData filterAxisMetaData;
4849
private final NamedList<Property> cellProperties =
4950
new ArrayNamedListImpl<Property>() {
50-
protected String getName(Property property) {
51-
return property.getName();
51+
public String elementName(Object property) {
52+
return ((Property) property).getName();
5253
}
5354
};
5455
final Map<String, Property> propertiesByTag;

‎src/org/olap4j/driver/xmla/XmlaOlap4jLevel.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ public Type getLevelType() {
181181

182182
public NamedList<Property> getProperties() {
183183
final NamedList<Property> list = new ArrayNamedListImpl<Property>() {
184-
protected String getName(Property property) {
185-
return property.getName();
184+
public String elementName(Object property) {
185+
return ((Property) property).getName();
186186
}
187187
};
188188
// standard properties first

‎src/org/olap4j/impl/AbstractNamedList.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222
import org.olap4j.metadata.NamedList;
2323

24-
import java.util.AbstractList;
24+
import java.util.*;
2525

2626
/**
2727
* Partial implementation of {@link org.olap4j.metadata.NamedList}.
2828
*
2929
* <p>Derived class must implement {@link #get(int)} and {@link #size()}, as
3030
* per {@link java.util.AbstractList}; and must implement
31-
* {@link #getName(Object)}, to indicate how elements are named.
31+
* {@link #elementName(Object)}, to indicate how elements are named.
3232
*
3333
* @see org.olap4j.impl.ArrayNamedListImpl
3434
*
@@ -40,11 +40,9 @@ public abstract class AbstractNamedList<T>
4040
extends AbstractList<T>
4141
implements NamedList<T>
4242
{
43-
protected abstract String getName(T t);
44-
4543
public T get(String name) {
4644
for (T t : this) {
47-
if (getName(t).equals(name)) {
45+
if (elementName(t).equals(name)) {
4846
return t;
4947
}
5048
}
@@ -54,12 +52,16 @@ public T get(String name) {
5452
public int indexOfName(String name) {
5553
for (int i = 0; i < size(); ++i) {
5654
T t = get(i);
57-
if (getName(t).equals(name)) {
55+
if (elementName(t).equals(name)) {
5856
return i;
5957
}
6058
}
6159
return -1;
6260
}
61+
62+
public Map<String, T> asMap() {
63+
return new NamedListMap<T>(this);
64+
}
6365
}
6466

6567
// End AbstractNamedList.java

‎src/org/olap4j/impl/ArrayNamedListImpl.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121

2222
import org.olap4j.metadata.NamedList;
2323

24-
import java.util.ArrayList;
25-
import java.util.Collection;
24+
import java.util.*;
2625

2726
/**
2827
* Implementation of {@link org.olap4j.metadata.NamedList} which uses
2928
* {@link java.util.ArrayList} for storage.
3029
*
31-
* <p>Derived class must implement {@link #getName(Object)}, to indicate how
30+
* <p>Derived class must implement {@link #elementName(Object)}, to indicate how
3231
* elements are named.
3332
*
3433
* @see NamedListImpl
@@ -71,11 +70,9 @@ public ArrayNamedListImpl(Collection<? extends T> c) {
7170
super(c);
7271
}
7372

74-
protected abstract String getName(T t);
75-
7673
public T get(String name) {
7774
for (T t : this) {
78-
if (getName(t).equals(name)) {
75+
if (elementName(t).equals(name)) {
7976
return t;
8077
}
8178
}
@@ -85,12 +82,16 @@ public T get(String name) {
8582
public int indexOfName(String name) {
8683
for (int i = 0; i < size(); ++i) {
8784
T t = get(i);
88-
if (getName(t).equals(name)) {
85+
if (elementName(t).equals(name)) {
8986
return i;
9087
}
9188
}
9289
return -1;
9390
}
91+
92+
public Map<String, T> asMap() {
93+
return new NamedListMap<T>(this);
94+
}
9495
}
9596

9697
// End ArrayNamedListImpl.java

‎src/org/olap4j/impl/NamedListImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public NamedListImpl(Collection<? extends T> c) {
6363
super(c);
6464
}
6565

66-
protected final String getName(T t) {
67-
return t.getName();
66+
public final String elementName(Object t) {
67+
return ((T) t).getName();
6868
}
6969
}
7070

‎src/org/olap4j/impl/NamedListMap.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
// $Id: ArrayNamedListImpl.java 482 2012-01-05 23:27:27Z jhyde $
3+
//
4+
// Licensed to Julian Hyde under one or more contributor license
5+
// agreements. See the NOTICE file distributed with this work for
6+
// additional information regarding copyright ownership.
7+
//
8+
// Julian Hyde licenses this file to you under the Apache License,
9+
// Version 2.0 (the "License"); you may not use this file except in
10+
// compliance with the License. You may obtain a copy of the License at:
11+
//
12+
// http://www.apache.org/licenses/LICENSE-2.0
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
*/
20+
package org.olap4j.impl;
21+
22+
import org.olap4j.metadata.NamedList;
23+
24+
import java.util.*;
25+
26+
/**
27+
* Map backed by a {@link org.olap4j.metadata.NamedList}.
28+
*
29+
* @author jhyde
30+
* @version $Id: AbstractNamedList.java 482 2012-01-05 23:27:27Z jhyde $
31+
*/
32+
class NamedListMap<T> extends AbstractMap<String, T> {
33+
private final NamedList<T> namedList;
34+
35+
/**
36+
* Creates a NamedListMap.
37+
*
38+
* @param namedList Named list
39+
*/
40+
public NamedListMap(NamedList<T> namedList) {
41+
this.namedList = namedList;
42+
}
43+
44+
public Set<Entry<String, T>> entrySet() {
45+
return new AbstractSet<Entry<String, T>>() {
46+
public Iterator<Entry<String, T>> iterator() {
47+
final Iterator<T> iterator = namedList.iterator();
48+
return new Iterator<Entry<String, T>>() {
49+
public boolean hasNext() {
50+
return iterator.hasNext();
51+
}
52+
53+
public Entry<String, T> next() {
54+
T x = iterator.next();
55+
String name = namedList.elementName(x);
56+
return new Pair<String, T>(name, x);
57+
}
58+
59+
public void remove() {
60+
iterator.remove();
61+
}
62+
};
63+
}
64+
65+
public int size() {
66+
return namedList.size();
67+
}
68+
};
69+
}
70+
}
71+
72+
// End NamedListMap.java

‎src/org/olap4j/impl/Olap4jUtil.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,8 @@ public static <T> NamedList<T> unmodifiableNamedList(
484484
final NamedList<? extends T> list)
485485
{
486486
return list instanceof RandomAccess
487-
? new UnmodifiableNamedRandomAccessList<T>(list)
488-
: new UnmodifiableNamedList<T>(list);
487+
? new UnmodifiableNamedRandomAccessList<T>(list)
488+
: new UnmodifiableNamedList<T>(list);
489489
}
490490

491491
/**
@@ -615,10 +615,10 @@ private enum DummyEnum {
615615
}
616616

617617
/**
618-
* Implementation of {@link NamedList} whih is immutable and empty.
618+
* Implementation of {@link NamedList} that is immutable and empty.
619619
*/
620620
private static class EmptyNamedList<T> extends AbstractNamedList<T> {
621-
protected String getName(Object o) {
621+
public String elementName(Object element) {
622622
throw new UnsupportedOperationException();
623623
}
624624

@@ -652,6 +652,14 @@ public int indexOfName(String s) {
652652
return list.indexOfName(s);
653653
}
654654

655+
public String elementName(Object element) {
656+
return list.elementName(element);
657+
}
658+
659+
public Map<String, T> asMap() {
660+
return Collections.unmodifiableMap(list.asMap());
661+
}
662+
655663
public int size() {
656664
return list.size();
657665
}

‎src/org/olap4j/metadata/NamedList.java

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.olap4j.metadata;
2121

2222
import java.util.List;
23+
import java.util.Map;
2324

2425
/**
2526
* Extension to {@link java.util.List} which allows access to members of the
@@ -54,6 +55,22 @@ public interface NamedList<E> extends List<E> {
5455
* @see #indexOf(Object)
5556
*/
5657
int indexOfName(String name);
58+
59+
/**
60+
* Returns the name of a given element.
61+
*
62+
* @param element Element
63+
* @return Name of element
64+
*/
65+
String elementName(Object element);
66+
67+
/**
68+
* Returns a view of this named list as a {@link Map} whose key is the name
69+
* of each element.
70+
*
71+
* @return A view of this named list as a map
72+
*/
73+
Map<String, E> asMap();
5774
}
5875

5976
// End NamedList.java

0 commit comments

Comments
 (0)
Please sign in to comment.