Lucene example source code file (TestCharArrayMap.java)
This example Lucene source code file (TestCharArrayMap.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.lucene.analysis;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import org.apache.lucene.util.LuceneTestCase;
public class TestCharArrayMap extends LuceneTestCase {
public void doRandom(int iter, boolean ignoreCase) {
CharArrayMap<Integer> map = new CharArrayMap(TEST_VERSION_CURRENT, 1, ignoreCase);
HashMap<String,Integer> hmap = new HashMap();
char[] key;
for (int i=0; i<iter; i++) {
int len = random.nextInt(5);
key = new char[len];
for (int j=0; j<key.length; j++) {
key[j] = (char)random.nextInt(127);
}
String keyStr = new String(key);
String hmapKey = ignoreCase ? keyStr.toLowerCase(Locale.ENGLISH) : keyStr;
int val = random.nextInt();
Object o1 = map.put(key, val);
Object o2 = hmap.put(hmapKey,val);
assertEquals(o1,o2);
// add it again with the string method
assertEquals(val, map.put(keyStr,val).intValue());
assertEquals(val, map.get(key,0,key.length).intValue());
assertEquals(val, map.get(key).intValue());
assertEquals(val, map.get(keyStr).intValue());
assertEquals(hmap.size(), map.size());
}
}
public void testCharArrayMap() {
int num = 5 * RANDOM_MULTIPLIER;
for (int i = 0; i < num; i++) { // pump this up for more random testing
doRandom(1000,false);
doRandom(1000,true);
}
}
public void testMethods() {
CharArrayMap<Integer> cm = new CharArrayMap(TEST_VERSION_CURRENT, 2, false);
HashMap<String,Integer> hm = new HashMap();
hm.put("foo",1);
hm.put("bar",2);
cm.putAll(hm);
assertEquals(hm.size(), cm.size());
hm.put("baz", 3);
cm.putAll(hm);
assertEquals(hm.size(), cm.size());
CharArraySet cs = cm.keySet();
int n=0;
for (Object o : cs) {
assertTrue(cm.containsKey(o));
char[] co = (char[]) o;
assertTrue(cm.containsKey(co, 0, co.length));
n++;
}
assertEquals(hm.size(), n);
assertEquals(hm.size(), cs.size());
assertEquals(cm.size(), cs.size());
cs.clear();
assertEquals(0, cs.size());
assertEquals(0, cm.size());
try {
cs.add("test");
fail("keySet() allows adding new keys");
} catch (UnsupportedOperationException ue) {
// pass
}
cm.putAll(hm);
assertEquals(hm.size(), cs.size());
assertEquals(cm.size(), cs.size());
Iterator<Map.Entry
Other Lucene examples (source code examples)
Here is a short list of links related to this Lucene TestCharArrayMap.java source code file: