Renaming WCat to USel. Some universe messing around.

This commit is contained in:
Olof Larsson 2012-09-02 14:13:20 +02:00
parent 4883d0754f
commit 5845a7c19b
8 changed files with 122 additions and 82 deletions

View File

@ -30,13 +30,19 @@ public class Coll<E, L> implements CollInterface<E, L>
// WHAT DO WE HANDLE?
// -------------------------------------------- //
protected String name;
protected final String name;
@Override public String name() { return this.name; }
protected Class<E> entityClass;
protected final String nameBase;
@Override public String nameBase() { return this.nameBase; }
protected final String nameUniverse;
@Override public String nameUniverse() { return this.nameUniverse; }
protected final Class<E> entityClass;
@Override public Class<E> entityClass() { return this.entityClass; }
protected Class<L> idClass;
protected final Class<L> idClass;
@Override public Class<L> idClass() { return this.idClass; }
// -------------------------------------------- //
@ -525,7 +531,19 @@ public class Coll<E, L> implements CollInterface<E, L>
public Coll(Db<?> db, MPlugin mplugin, String idStrategyName, String name, Class<E> entityClass, Class<L> idClass, boolean creative)
{
// Setup the name and the parsed parts
this.name = name;
String[] nameParts = this.name.split("\\@");
this.nameBase = nameParts[0];
if (nameParts.length > 1)
{
this.nameUniverse = nameParts[1];
}
else
{
this.nameUniverse = null;
}
this.entityClass = entityClass;
this.idClass = idClass;
this.creative = creative;

View File

@ -15,6 +15,8 @@ public interface CollInterface<E, L>
// WHAT DO WE HANDLE?
// -------------------------------------------- //
public String name();
public String nameBase();
public String nameUniverse();
public Class<E> entityClass();
public Class<L> idClass();

View File

@ -7,20 +7,39 @@ import com.massivecraft.mcore4.util.MUtil;
public abstract class Colls<C extends Coll<E, L>, E, L>
{
public abstract String name();
public abstract C createColl(String collName);
protected Map<String, C> name2coll = new HashMap<String, C>();
public String collNameFromCategory(String category)
public abstract C createColl(String name);
public abstract String getContext();
public String universeFromWorldName(String worldName)
{
return this.name() + "_" + category;
USel selector = USelColl.i.get(this.getContext());
return selector.select(worldName);
}
protected Map<String, C> name2coll = new HashMap<String, C>();
public C get(Object worldNameExtractable)
{
String worldName = MUtil.extract(String.class, "worldName", worldNameExtractable);
String category = WCatColl.i.get(this.name()).categorize(worldName);
String collName = this.collNameFromCategory(category);
return this.getForWorld(worldName);
}
public E get2(Object worldNameExtractable)
{
C coll = this.get(worldNameExtractable);
if (coll == null) return null;
return coll.get(worldNameExtractable);
}
public C getForWorld(String worldName)
{
return this.getForUniverse(this.universeFromWorldName(worldName));
}
public C getForUniverse(String universe)
{
String collName = this.getContext() + "@" + universe;
C ret = this.name2coll.get(collName);
if (ret == null)
@ -31,11 +50,4 @@ public abstract class Colls<C extends Coll<E, L>, E, L>
return ret;
}
public E get2(Object worldNameExtractable)
{
C coll = this.get(worldNameExtractable);
if (coll == null) return null;
return coll.get(worldNameExtractable);
}
}

View File

@ -23,6 +23,14 @@ public abstract class Entity<E extends Entity<E, L>, L>
public abstract E getDefaultInstance();
public String getUniverse()
{
Coll<E, L> coll = this.getColl();
if (coll == null) return null;
return coll.nameUniverse();
}
public L attach(Coll<E, L> coll)
{
return coll.attach(getThis());

View File

@ -8,46 +8,46 @@ import java.util.List;
import com.massivecraft.mcore4.util.MUtil;
/**
* WCat stands for World Categorizer.
* They provide a recursive matching system.
* USel stands for "Universe Selector".
* The task of a USel is to select a "universe" based on a world.
* There will be one USel per "context".
*/
public class WCat extends Entity<WCat, String>
public class USel extends Entity<USel, String>
{
public final static transient String RETURN = "_return";
public final static transient String RUN = "_run";
public final static transient String _DEFAULT = "_default";
public final static transient String DEFAULT = "default";
public final static transient List<WCatRule> DEFAULT_RULES = MUtil.list(new WCatRule(RUN, _DEFAULT));
public final static transient List<WCatRule> DEFAULT_DEFAULT_RULES = MUtil.list(new WCatRule(RETURN, DEFAULT));
public final static transient List<USelRule> DEFAULT_RULES = MUtil.list(new USelRule(RUN, _DEFAULT));
public final static transient List<USelRule> DEFAULT_DEFAULT_RULES = MUtil.list(new USelRule(RETURN, DEFAULT));
// -------------------------------------------- //
// META
// -------------------------------------------- //
@Override public Coll<WCat, String> getColl() { return WCatColl.i; }
@Override protected WCat getThis() { return this; }
@Override protected USel getThis() { return this; }
private final static transient WCat defaultInstance = new WCat();
@Override public WCat getDefaultInstance(){ return defaultInstance; }
@Override protected Class<WCat> getClazz() { return WCat.class; }
private final static transient USel defaultInstance = new USel();
@Override public USel getDefaultInstance(){ return defaultInstance; }
@Override protected Class<USel> getClazz() { return USel.class; }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected List<WCatRule> rules;
public List<WCatRule> rules() { return this.rules; };
public void rules(List<WCatRule> val) { this.rules = new ArrayList<WCatRule>(val); };
protected List<USelRule> rules;
public List<USelRule> rules() { return this.rules; };
public void rules(List<USelRule> val) { this.rules = new ArrayList<USelRule>(val); };
public void rules(String... namesAndParams)
{
this.rules = new ArrayList<WCatRule>();
this.rules = new ArrayList<USelRule>();
Iterator<String> iter = Arrays.asList(namesAndParams).iterator();
while (iter.hasNext())
{
String name = iter.next();
String param = iter.next();
this.rules.add(new WCatRule(name, param));
this.rules.add(new USelRule(name, param));
}
}
@ -55,7 +55,7 @@ public class WCat extends Entity<WCat, String>
// CONSTRUCT
// -------------------------------------------- //
public WCat()
public USel()
{
this.rules(DEFAULT_RULES);
}
@ -64,9 +64,9 @@ public class WCat extends Entity<WCat, String>
// THAT SPECIAL LOGIC
// -------------------------------------------- //
public String categorize(String worldName)
public String select(String worldName)
{
for (WCatRule rule : this.rules())
for (USelRule rule : this.rules())
{
String name = rule.name();
String param = rule.param();
@ -76,9 +76,9 @@ public class WCat extends Entity<WCat, String>
}
else if (name.equals(RUN))
{
WCat subcat = WCatColl.i.get(param);
String subcatresult = subcat.categorize(worldName);
if (subcatresult != null) return subcatresult;
USel subSelector = USelColl.i.get(param);
String subSelectorResult = subSelector.select(worldName);
if (subSelectorResult != null) return subSelectorResult;
}
else if (name.equalsIgnoreCase(worldName))
{

View File

@ -0,0 +1,41 @@
package com.massivecraft.mcore4.store;
import com.massivecraft.mcore4.MCore;
public class USelColl extends Coll<USel, String>
{
// -------------------------------------------- //
// META
// -------------------------------------------- //
public static USelColl i = new USelColl();
private USelColl()
{
super(MCore.p, "ai", "mcore_usel", USel.class, String.class, true);
}
@Override
public void copy(Object ofrom, Object oto)
{
USel from = (USel)ofrom;
USel to = (USel)oto;
to.rules = from.rules;
}
@Override
public void init()
{
super.init();
// Ensure the default WorldCategorizer is present.
USel d = this.get(USel._DEFAULT);
d.rules(USel.DEFAULT_DEFAULT_RULES);
}
@Override
public boolean isDefault(USel entity)
{
return entity.rules().equals(USel.DEFAULT_RULES);
}
}

View File

@ -1,6 +1,6 @@
package com.massivecraft.mcore4.store;
public class WCatRule
public class USelRule
{
protected final String name;
public String name() { return this.name; }
@ -8,13 +8,13 @@ public class WCatRule
protected final String param;
public String param() { return this.param; }
public WCatRule()
public USelRule()
{
this.name = null;
this.param = null;
}
public WCatRule(String name, String param)
public USelRule(String name, String param)
{
this.name = name;
this.param = param;

View File

@ -1,41 +0,0 @@
package com.massivecraft.mcore4.store;
import com.massivecraft.mcore4.MCore;
public class WCatColl extends Coll<WCat, String>
{
// -------------------------------------------- //
// META
// -------------------------------------------- //
public static WCatColl i = new WCatColl();
private WCatColl()
{
super(MCore.p, "ai", "mcore_wcat", WCat.class, String.class, true);
}
@Override
public void copy(Object ofrom, Object oto)
{
WCat from = (WCat)ofrom;
WCat to = (WCat)oto;
to.rules = from.rules;
}
@Override
public void init()
{
super.init();
// Ensure the default WorldCategorizer is present.
WCat d = this.get(WCat._DEFAULT);
d.rules(WCat.DEFAULT_DEFAULT_RULES);
}
@Override
public boolean isDefault(WCat entity)
{
return entity.rules().equals(WCat.DEFAULT_RULES);
}
}