SimpleMemoryCollection.java

/***************************************************************************
   Copyright 2012 Emily Estes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***************************************************************************/
package net.metanotion.contentstore;


import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.metanotion.util.Dictionary;
import net.metanotion.util.Pair;

/** This is an "in-memory"/"reference implementation" of the semantics of the SimpleCollection interface.
	It is not meant to be used in production, and is primarily useful for testing and debugging purposes.
	@param <O> The type of objects stored in the collection.
*/
public final class SimpleMemoryCollection<O> implements Dictionary<String,SimpleCollection<O>> {
	private static final Logger logger = LoggerFactory.getLogger(SimpleMemoryCollection.class);

	/** An implementation of SimpleCollection backed by {@link java.util.concurrent.ConcurrentHashMap}. */
	public static final class MemoryCollection<O> implements SimpleCollection<O> {
		/* Crappy implementation of test code for sync library. */
		private final AtomicInteger ctr = new AtomicInteger();
		private final ConcurrentHashMap<Integer,O> map = new ConcurrentHashMap();

		@Override public Iterable<Map.Entry<String,O>> list(final int maxPageSize, final int offset) {
			final ArrayList<Map.Entry<String,O>> result = new ArrayList<>(maxPageSize);
			int i=0;
			int count=0;
			for(Map.Entry<Integer,O> ix: map.entrySet()) {
				if((i <= offset) && (count < maxPageSize)) {
					result.add(new Pair<String,O>(ix.getKey().toString(), ix.getValue()));
					count++;
				}
				i++;
			}
			return result;
		}

		@Override public O get(final String index) { return map.get(Integer.parseInt(index)); }

		@Override public String create(final O o) {
			final int ix = ctr.getAndIncrement();
			map.put(ix, o);
			return Integer.toString(ix);
		}

		@Override public Map.Entry<String,O> update(final String index, final O o) {
			return new Pair<String,O>(index, map.put(Integer.parseInt(index), o));
		}
		@Override public boolean remove(final String index) {
			return (map.remove(Integer.parseInt(index)) != null) ? true : false;
		}
	};

	private final SimpleCollection<O> db = new MemoryCollection<O>();
	@Override public SimpleCollection<O> get(final String collection) { return db; }
}