All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.maxifier.mxcache.impl.caches.batch.CollectionKeyStrategy Maven / Gradle / Ivy

Go to download

Constains all classes necessary for launching a MxCache-instrumentated application

There is a newer version: 2.6.9
Show newest version
/*
 * 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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy