Mercurial > hg > Members > shoshi > TreeCMSv2
view src/treecms/memory/OnMemoryForest.java @ 3:5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
author | shoshi |
---|---|
date | Fri, 18 Feb 2011 02:14:10 +0900 |
parents | 4a5ee88f02cf |
children | f5ed85be5640 |
line wrap: on
line source
package treecms.memory; import java.util.Random; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import treecms.api.Forest; import treecms.api.Node; import treecms.api.NodeID; import treecms.tree.id.RandomNodeID; public class OnMemoryForest implements Forest { ConcurrentHashMap<NodeID,OnMemoryNode> m_table; public OnMemoryForest() { m_table = new ConcurrentHashMap<NodeID,OnMemoryNode>(); } public OnMemoryNode createNode(NodeID _id) { OnMemoryNode newNode; if(_id == null){ newNode = new OnMemoryNode(this,createID()); }else{ newNode = new OnMemoryNode(this,_id); } m_table.put(newNode.getID(),newNode); return newNode; } NodeID createID() { return new RandomNodeIDImpl(null); } @Override public Node get(NodeID _id) { return m_table.get(_id); } @Override public Node create() { return createNode(null); } class RandomNodeIDImpl extends RandomNodeID { String m_uuid; long m_version; public RandomNodeIDImpl(String _uuid) { if(_uuid != null){ m_uuid = _uuid; }else{ m_uuid = UUID.randomUUID().toString(); } m_version = (new Random()).nextLong(); } @Override public NodeID create() { return new RandomNodeIDImpl(null); } @Override public NodeID update() { return new RandomNodeIDImpl(m_uuid); } @Override public String getUUID() { return m_uuid; } @Override public String getVersion() { return Long.toHexString(m_version); } } }