* Lazy-loading values for given keys
*/
public Iterable<V> values(Object firstKey, Object secondKey) {
- try {
- exchange.clear();
- exchange.append(firstKey).append(secondKey).append(Key.BEFORE);
- Exchange iteratorExchange = new Exchange(exchange);
- KeyFilter filter = new KeyFilter().append(KeyFilter.simpleTerm(firstKey)).append(KeyFilter.simpleTerm(secondKey));
- return new ValueIterable<V>(iteratorExchange, filter);
- } catch (Exception e) {
- throw failToGetValues(e);
- }
+ return new ValueIterable<V>(exchange, firstKey, secondKey);
}
private IllegalStateException failToGetValues(Exception e) {
* Lazy-loading values for a given key
*/
public Iterable<V> values(Object firstKey) {
- try {
- exchange.clear();
- exchange.append(firstKey).append(Key.BEFORE);
- Exchange iteratorExchange = new Exchange(exchange);
- KeyFilter filter = new KeyFilter().append(KeyFilter.simpleTerm(firstKey));
- return new ValueIterable<V>(iteratorExchange, filter);
- } catch (Exception e) {
- throw failToGetValues(e);
- }
+ return new ValueIterable<V>(exchange, firstKey);
}
/**
* Lazy-loading values
*/
public Iterable<V> values() {
- try {
- exchange.clear().append(Key.BEFORE);
- Exchange iteratorExchange = new Exchange(exchange);
- KeyFilter filter = new KeyFilter().append(KeyFilter.ALL);
- return new ValueIterable<V>(iteratorExchange, filter);
- } catch (Exception e) {
- throw failToGetValues(e);
- }
+ return new ValueIterable<V>(exchange);
}
public Iterable<Entry<V>> entries() {
- exchange.clear().to(Key.BEFORE);
- KeyFilter filter = new KeyFilter().append(KeyFilter.ALL);
- return new EntryIterable<V>(new Exchange(exchange), filter);
+ return new EntryIterable<V>(exchange);
}
public Iterable<Entry<V>> entries(Object firstKey) {
- exchange.clear().append(firstKey).append(Key.BEFORE);
- KeyFilter filter = new KeyFilter().append(KeyFilter.simpleTerm(firstKey));
- return new EntryIterable<V>(new Exchange(exchange), filter);
+ return new EntryIterable<V>(exchange, firstKey);
}
private void resetKey(Object key) {
//
private static class ValueIterable<T> implements Iterable<T> {
- private final Iterator<T> iterator;
+ private final Exchange originExchange;
+ private final Object[] keys;
- private ValueIterable(Exchange exchange, KeyFilter keyFilter) {
- this.iterator = new ValueIterator<T>(exchange, keyFilter);
+ private ValueIterable(Exchange originExchange, Object... keys) {
+ this.originExchange = originExchange;
+ this.keys = keys;
}
@Override
public Iterator<T> iterator() {
- return iterator;
+ originExchange.clear();
+ KeyFilter filter = new KeyFilter();
+ for (Object key : keys) {
+ originExchange.append(key);
+ filter = filter.append(KeyFilter.simpleTerm(key));
+ }
+ originExchange.append(Key.BEFORE);
+ Exchange iteratorExchange = new Exchange(originExchange);
+ return new ValueIterator<T>(iteratorExchange, filter);
}
}
}
private static class EntryIterable<T> implements Iterable<Entry<T>> {
- private final EntryIterator<T> it;
+ private final Exchange originExchange;
+ private final Object[] keys;
- private EntryIterable(Exchange exchange, KeyFilter keyFilter) {
- it = new EntryIterator<T>(exchange, keyFilter);
+ private EntryIterable(Exchange originExchange, Object... keys) {
+ this.originExchange = originExchange;
+ this.keys = keys;
}
@Override
public Iterator<Entry<T>> iterator() {
- return it;
+ originExchange.clear();
+ KeyFilter filter = new KeyFilter();
+ for (Object key : keys) {
+ originExchange.append(key);
+ filter = filter.append(KeyFilter.simpleTerm(key));
+ }
+ originExchange.append(Key.BEFORE);
+ Exchange iteratorExchange = new Exchange(originExchange);
+ return new EntryIterator<T>(iteratorExchange, filter);
}
}
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.sonar.batch.index.Cache.Entry;
import static org.fest.assertions.Assertions.assertThat;
assertThat(cache.get("france")).isEqualTo("paris");
assertThat(cache.keySet()).containsOnly("france", "italy");
assertThat(cache.keySet("france")).isEmpty();
- assertThat(cache.values()).containsOnly("paris", "rome");
+ Iterable<String> values = cache.values();
+ assertThat(values).containsOnly("paris", "rome");
+ assertThat(values).containsOnly("paris", "rome");
assertThat(cache.containsKey("france")).isTrue();
- Cache.Entry[] entries = Iterables.toArray(cache.entries(), Cache.Entry.class);
+ Iterable<Entry<String>> iterable = cache.entries();
+ Cache.Entry[] entries = Iterables.toArray(iterable, Cache.Entry.class);
assertThat(entries).hasSize(2);
+ assertThat(iterable).hasSize(2);
assertThat(entries[0].key()[0]).isEqualTo("france");
assertThat(entries[0].value()).isEqualTo("paris");
assertThat(entries[1].key()[0]).isEqualTo("italy");
assertThat(cache.keySet("france")).isEmpty();
assertThat(cache.containsKey("france")).isFalse();
assertThat(cache.containsKey("italy")).isTrue();
- assertThat(cache.values()).containsOnly("rome");
+ assertThat(values).containsOnly("rome");
cache.clear();
- assertThat(cache.values()).isEmpty();
+ assertThat(values).isEmpty();
}
@Test
assertThat(cache.values("europe")).containsOnly("paris", "rome");
assertThat(cache.values("oceania")).isEmpty();
- Cache.Entry[] allEntries = Iterables.toArray(cache.entries(), Cache.Entry.class);
+ Iterable<Entry<String>> iterable = cache.entries();
+ Cache.Entry[] allEntries = Iterables.toArray(iterable, Cache.Entry.class);
assertThat(allEntries).hasSize(3);
+ assertThat(iterable).hasSize(3);
assertThat(allEntries[0].key()).isEqualTo(new String[] {"asia", "china"});
assertThat(allEntries[0].value()).isEqualTo("pekin");
assertThat(allEntries[1].key()).isEqualTo(new String[] {"europe", "france"});
assertThat(allEntries[2].key()).isEqualTo(new String[] {"europe", "italy"});
assertThat(allEntries[2].value()).isEqualTo("rome");
- Cache.Entry[] subEntries = Iterables.toArray(cache.entries("europe"), Cache.Entry.class);
+ Iterable<Entry<String>> iterable2 = cache.entries("europe");
+ Cache.Entry[] subEntries = Iterables.toArray(iterable2, Cache.Entry.class);
assertThat(subEntries).hasSize(2);
+ assertThat(iterable2).hasSize(2);
assertThat(subEntries[0].key()).isEqualTo(new String[] {"europe", "france"});
assertThat(subEntries[0].value()).isEqualTo("paris");
assertThat(subEntries[1].key()).isEqualTo(new String[] {"europe", "italy"});
assertThat(cache.values("europe")).containsOnly("eiffel tower", "lake", "colosseum", "notre dame");
assertThat(cache.values("europe", "france")).containsOnly("eiffel tower", "lake", "notre dame");
- Cache.Entry[] allEntries = Iterables.toArray(cache.entries(), Cache.Entry.class);
+ Iterable<Entry<String>> iterable = cache.entries();
+ Cache.Entry[] allEntries = Iterables.toArray(iterable, Cache.Entry.class);
assertThat(allEntries).hasSize(7);
+ assertThat(iterable).hasSize(7);
assertThat(allEntries[0].key()).isEqualTo(new String[] {"america", "us", "new york"});
assertThat(allEntries[0].value()).isEqualTo("empire state building");
assertThat(allEntries[1].key()).isEqualTo(new String[] {"asia", "china", "pekin"});
assertThat(allEntries[5].key()).isEqualTo(new String[] {"europe", "italy", "rome"});
assertThat(allEntries[5].value()).isEqualTo("colosseum");
- Cache.Entry[] subEntries = Iterables.toArray(cache.entries("europe"), Cache.Entry.class);
+ Iterable<Entry<String>> iterable2 = cache.entries("europe");
+ Cache.Entry[] subEntries = Iterables.toArray(iterable2, Cache.Entry.class);
assertThat(subEntries).hasSize(4);
+ assertThat(iterable2).hasSize(4);
assertThat(subEntries[0].key()).isEqualTo(new String[] {"europe", "france", "annecy"});
assertThat(subEntries[0].value()).isEqualTo("lake");
assertThat(subEntries[1].key()).isEqualTo(new String[] {"europe", "france", "paris"});