com.maxifier.mxcache.impl.caches.batch.CollectionKeyStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mxcache-runtime Show documentation
Show all versions of mxcache-runtime Show documentation
Constains all classes necessary for launching a MxCache-instrumentated application
/*
* Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
*/
package com.maxifier.mxcache.impl.caches.batch;
import gnu.trove.set.hash.THashSet;
import java.util.*;
/**
* @author Alexander Kochurov ([email protected])
*/
final class CollectionKeyStrategy implements KeyStrategy, T, Iterator> {
private CollectionKeyStrategy() {}
private static final CollectionKeyStrategy INSTANCE = new CollectionKeyStrategy();
public static CollectionKeyStrategy getInstance() {
//noinspection unchecked
return INSTANCE;
}
@Override
public int size(Collection key) {
return key.size();
}
@Override
public Iterator iterator(Collection key) {
return key.iterator();
}
@Override
public T get(int order, Iterator iterator) {
return iterator.next();
}
@Override
public Collection toKey(Collection key, int n) {
return key;
}
@Override
public Collection create(Collection expectedType) {
if (expectedType instanceof SortedSet) {
return new TreeSet();
}
if (expectedType instanceof Set) {
return new THashSet(expectedType.size());
}
return new ArrayList(expectedType.size());
}
@Override
public boolean isStableOrder() {
return false;
}
@Override
public boolean put(Collection key, int index, T value) {
return key.add(value);
}
}