@Override public Collection<Type> call() throws Exception { return fqnIndexer.findAll(); } });
@Override public Collection<Type> call() throws Exception { return fqnIndexer.findByPattern(pattern); } });
@Override public ImmutableType call() throws Exception { return fqnIndexer.lookup(fqn); } });
/** * Finds type by exact FQN name. * * @param fqn * Fully qualified class name. * @return Returns type or <code>null</code> if it can not be found. */ public E lookup(String fqn) { int index = findRetrieveIndexFor(fqn); return getAt(index); }
/** * Finds all {@link NonPrimitiveType}s that start with given string. * * @param fqnWildCard * String that class should start with. * @return All types starting with the given string. */ public Collection<E> findStartsWith(String fqnWildCard) { long minMaxIndex = findStartsWithMinMaxIndexes(fqnWildCard); int min = getLowerInt(minMaxIndex); int max = getUpperInt(minMaxIndex); if (min < 0) { return Collections.emptyList(); } int size = size(); List<E> results = new ArrayList<>((max - min) + 1); for (int i = min; (i <= max) && (max < size); i++) { results.add(getAt(i)); } return results; }
@Test public void sameFqnTwice() { ClassType stringType1 = new ClassType(String.class.getName()); ClassType stringType2 = new ClassType(String.class.getName()); indexer.index(stringType1); indexer.index(stringType2); ClassType lookup = indexer.lookup(String.class.getName()); assertThat(indexer, hasSize(1)); assertThat(lookup == stringType2, is(true)); }
@Test public void wildcardPattern() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(new ClassType("a")); indexer.index(new ClassType("java.nolang")); indexer.index(objectType); WildcardMatchPattern wildcardMatchPattern = new WildcardMatchPattern("java*lang"); Collection<ClassType> results = indexer.findByPattern(wildcardMatchPattern); assertThat(results, hasSize(1)); for (ClassType classType : results) { assertThat(wildcardMatchPattern.match(classType.getFQN()), is(true)); } }
@Test public void notFound() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(new ClassType("a")); indexer.index(new ClassType("java.nolang")); indexer.index(objectType); // middle Collection<ClassType> results = indexer.findStartsWith("aa"); assertThat(results, is(empty())); results = indexer.findStartsWith("java.lang.something"); assertThat(results, is(empty())); results = indexer.findStartsWith("ww"); assertThat(results, is(empty())); } }
int size = size(); int minIndex = -1; int maxIndex = -1; int mid = midpoint(min, max); String fqn = getAt(mid).getFQN(); if (fqn.startsWith(fqnWildCard)) { return pack(0, -1); int mid = midpoint(min, max); int compare = getAt(mid).getFQN().startsWith(fqnWildCard) ? 1 : -1; int mid = midpoint(min, max); int compare = getAt(mid).getFQN().startsWith(fqnWildCard) ? -1 : 1; return pack(maxIndex, minIndex);
results = findStartsWith(startsWithCriteria); E type = lookup(template); if (null != type) { results = new ArrayList<>(1);
@Test public void findAll() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(objectType); Collection<ClassType> types = indexer.findAll(); assertThat(types, hasSize(3)); assertThat(types, hasItem(stringType)); assertThat(types, hasItem(objectType)); assertThat(types, hasItem(thisType)); } }
/** * Finds index for a FQN to retrieve. * * @param fqn * String representing the FQN. * @return Index where element should be retrieved from or negative value if one can not be * located. */ private int findRetrieveIndexFor(String fqn) { int size = size(); int min = 0; int max = size - 1; while (max >= min) { int mid = midpoint(min, max); int compare = getAt(mid).getFQN().compareTo(fqn); // if no difference then we have it if (0 == compare) { return mid; } // otherwise adapt min and max min = (compare < 0) ? mid + 1 : min; max = (compare > 0) ? mid - 1 : max; } return -1; }
/** * Index one type. * * @param type * Type to index. */ void index(E type) { addOrUpdate(type); }
@Test public void notFound() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(objectType); ClassType lookup = indexer.lookup("nonExistingType"); assertThat(indexer, hasSize(3)); assertThat(lookup, is(nullValue())); }
@Test public void wildcardPatternStarFirstLast() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(new ClassType("a")); indexer.index(new ClassType("java.nolang")); indexer.index(objectType); WildcardMatchPattern wildcardMatchPattern = new WildcardMatchPattern("*lang*"); Collection<ClassType> results = indexer.findByPattern(wildcardMatchPattern); assertThat(results, hasSize(3)); for (ClassType classType : results) { assertThat(wildcardMatchPattern.match(classType.getFQN()), is(true)); } }
@Test public void found() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(new ClassType("a")); indexer.index(new ClassType("java.nolang")); indexer.index(objectType); // middle Collection<ClassType> results = indexer.findStartsWith("java.lang"); assertThat(results, hasSize(2)); for (ClassType classType : results) { assertThat(classType.getFQN().startsWith("java.lang"), is(true)); } // end results = indexer.findStartsWith("java.nolang"); assertThat(results, hasSize(1)); for (ClassType classType : results) { assertThat(classType.getFQN().startsWith("java.nolang"), is(true)); } // begin results = indexer.findStartsWith("a"); assertThat(results, hasSize(1)); for (ClassType classType : results) { assertThat(classType.getFQN().startsWith("a"), is(true)); } }
@Test public void equalsPatternNonExistingElement() { EqualsMatchPattern equalsMatchPattern = new EqualsMatchPattern("whatever"); Collection<ClassType> results = indexer.findByPattern(equalsMatchPattern); assertThat(results, is(empty())); } }
@Test public void replace() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); ClassType secondObjectType = new ClassType(Object.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(objectType); indexer.index(secondObjectType); ClassType lookup = indexer.lookup(Object.class.getName()); assertThat(indexer, hasSize(3)); assertThat(System.identityHashCode(lookup), is(System.identityHashCode(secondObjectType))); } }