com.maxifier.mxcache.impl.caches.batch.ArrayValueStrategy 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 java.lang.reflect.Array;
import java.util.BitSet;
/**
* @author Alexander Kochurov ([email protected])
*/
final class ArrayValueStrategy implements ValueStrategy {
private ArrayValueStrategy() {}
private static final ArrayValueStrategy INSTANCE = new ArrayValueStrategy();
public static ArrayValueStrategy getInstance() {
//noinspection unchecked
return INSTANCE;
}
@SuppressWarnings("MismatchedReadAndWriteOfArray")
@Override
public V[] compose(V[] knownValue, V[] calculated, BitSet composition) {
for (int i = composition.nextSetBit(0), p = 0; i >= 0; i = composition.nextSetBit(i+1), p++) {
knownValue[i] = calculated[p];
}
return knownValue;
}
@Override
public V[] createValue(Class valueType, int n) {
//noinspection unchecked
return (V[]) Array.newInstance(valueType.getComponentType(), n);
}
@Override
public BitSet createComposer(int n) {
return new BitSet(n);
}
@Override
public void addUnknown(V[] knownValues, BitSet composer, int i, K k) {
composer.set(i);
}
@Override
public void addKnown(V[] knownValues, BitSet composer, int i, K k, V v) {
knownValues[i] = v;
}
@Override
public V get(V[] value, int index, K key) {
return value[index];
}
@Override
public boolean requiresOrder() {
return true;
}
}