summaryrefslogtreecommitdiff
path: root/src/main/java/net/cshift/api/transit
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/cshift/api/transit')
-rw-r--r--src/main/java/net/cshift/api/transit/Transit.java7
-rw-r--r--src/main/java/net/cshift/api/transit/network/Channel.java12
-rw-r--r--src/main/java/net/cshift/api/transit/network/INode.java20
-rw-r--r--src/main/java/net/cshift/api/transit/network/ISystem.java15
-rw-r--r--src/main/java/net/cshift/api/transit/network/PoolManifest.java28
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java14
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java13
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java10
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java44
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java35
-rw-r--r--src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java6
-rw-r--r--src/main/java/net/cshift/api/transit/type/SimpleTypes.java8
-rw-r--r--src/main/java/net/cshift/api/transit/type/Type.java74
-rw-r--r--src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java88
-rw-r--r--src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java8
-rw-r--r--src/main/java/net/cshift/api/transit/type/group/TypeGroup.java16
16 files changed, 214 insertions, 184 deletions
diff --git a/src/main/java/net/cshift/api/transit/Transit.java b/src/main/java/net/cshift/api/transit/Transit.java
index adc9f25..6d0523d 100644
--- a/src/main/java/net/cshift/api/transit/Transit.java
+++ b/src/main/java/net/cshift/api/transit/Transit.java
@@ -26,10 +26,9 @@ package net.cshift.api.transit;
import net.fabricmc.api.ModInitializer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.slf4j.*;
-import net.cshift.api.transit.type.group.SimpleGroups;
+import net.cshift.api.transit.type.group.*;
/**
* @author Kyle Gunger
@@ -38,7 +37,7 @@ import net.cshift.api.transit.type.group.SimpleGroups;
public class Transit implements ModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger("Transit API");
-
+
@Override
public void onInitialize() {
LOGGER.info("Transit API: Providing a thin wrapper over reality!");
diff --git a/src/main/java/net/cshift/api/transit/network/Channel.java b/src/main/java/net/cshift/api/transit/network/Channel.java
index bd4cdf2..3fdae88 100644
--- a/src/main/java/net/cshift/api/transit/network/Channel.java
+++ b/src/main/java/net/cshift/api/transit/network/Channel.java
@@ -25,6 +25,8 @@
package net.cshift.api.transit.network;
import net.cshift.api.transit.network.packet.IStaticPacket;
+import net.cshift.api.transit.type.group.GroupRegistry;
+import net.cshift.api.transit.type.group.TypeGroup;
/**
* @author Kyle Gunger
@@ -35,6 +37,7 @@ public final class Channel<D> {
private INode to;
private int id;
private String group;
+ private Class<D> baseClass;
/** This constructor should be called by a node approving a connection. The approving node can give the connection an ID and group.
* Negative IDs indicate a terminated connection, so do not initialize the class with a negative ID.
@@ -43,11 +46,12 @@ public final class Channel<D> {
* @param id The channel's id, as assigned by the recieving node. In most cases, this will match the pool ID as a way to match channels to pools.
* @param group
*/
- public Channel(INode node, int id, String group)
+ public Channel(INode node, int id, String group, Class<D> baseClass)
{
to = node;
this.id = id;
this.group = group;
+ this.baseClass = baseClass;
}
@@ -78,8 +82,8 @@ public final class Channel<D> {
*
* @return
*/
- public String getGroup() {
- return group;
+ public TypeGroup<D> getGroup() {
+ return GroupRegistry.<D>groupByID(group, baseClass);
}
/** Returns true if the connection has been terminated
@@ -135,7 +139,7 @@ public final class Channel<D> {
* @param packet the packet to send
* @return The overflow data if the packet is only partially accepted. {@code null} otherwise.
*/
- public IStaticPacket<D> send(IStaticPacket<D> packet)
+ public IStaticPacket send(IStaticPacket packet)
{
return to.accept(packet, this);
}
diff --git a/src/main/java/net/cshift/api/transit/network/INode.java b/src/main/java/net/cshift/api/transit/network/INode.java
index 54701e7..6653e40 100644
--- a/src/main/java/net/cshift/api/transit/network/INode.java
+++ b/src/main/java/net/cshift/api/transit/network/INode.java
@@ -13,25 +13,29 @@ public interface INode
*/
public PoolManifest getManifest();
- /** Get the system managing the node or {@code null} if there isn't one
- *
- */
- public ISystem getSystem();
-
// ###############
// # Connections #
// ###############
- /** Call this function to establish a connection with a node.
+ /** Call this function to establish a specific connection with a node.
*
* @param <T> The type of connection being asked for
* @param poolID The ID of the pool the channel will interface with (see PoolManifest)
* @param asker The asking node
* @return A channel if the node accepts the request, {@code null} otherwise
*/
- public <T> Channel<T> connect(int poolID, INode asker);
+ public <T> Channel<T> connect(int poolID, String group, INode asker);
+
+ /** Call this function to establish a default connection with a node.
+ *
+ * @param <T> The type of connection being asked for
+ * @param group The group that is being connected to
+ * @param asker The asking node
+ * @return A channel if the node accepts the request, {@code null} otherwise
+ */
+ public <T> Channel<T> connectDefault(String group, INode asker);
/** Accept a packet from a channel (or not).
*
@@ -41,7 +45,7 @@ public interface INode
* @param channel The channel which the packet is coming through
* @return The overflow data if the packet is only partially accepted. {@code null} otherwise.
*/
- public <T> IStaticPacket<T> accept(IStaticPacket<T> packet, Channel<T> channel);
+ public IStaticPacket accept(IStaticPacket packet, Channel<?> channel);
/** Pressure
*
diff --git a/src/main/java/net/cshift/api/transit/network/ISystem.java b/src/main/java/net/cshift/api/transit/network/ISystem.java
deleted file mode 100644
index b7ab9bf..0000000
--- a/src/main/java/net/cshift/api/transit/network/ISystem.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package net.cshift.api.transit.network;
-
-
-/**
- * @author Kyle Gunger
- * @apiNote An ISystem represents a system of nodes which has been optimized for performance. A node can exist on its own, but an ISystem can't exist without a node.
- */
-public interface ISystem
-{
- /**The nodes stored by the system
- *
- * @return INode[]
- */
- public INode[] getNodes();
-}
diff --git a/src/main/java/net/cshift/api/transit/network/PoolManifest.java b/src/main/java/net/cshift/api/transit/network/PoolManifest.java
index 26cbece..e539ee1 100644
--- a/src/main/java/net/cshift/api/transit/network/PoolManifest.java
+++ b/src/main/java/net/cshift/api/transit/network/PoolManifest.java
@@ -6,35 +6,37 @@ package net.cshift.api.transit.network;
*/
public abstract class PoolManifest {
- /** Represents the number of pools that the node has access to for the specified resource.
- * @apiNote A "pool" in this context represents an independant network of resources.
+ /** Represents the number of pools that the node has access to.
+ * @apiNote A "pool" in this context represents an independent network of resources.
+ * Each network in the pool may represent a different TypeGroup.
* Pool zero should be the default group that simple nodes will attempt to connect to.
* @param group The TypeGroup that the pool belongs to
*/
- public abstract int poolCount(String group);
+ public abstract int poolCount();
- /** The ID of the pool. The INode will use this in a connection attempt with the other INode.
- *
- * @param group The TypeGroup the pool belongs to
- * @param pool Array-like index for pool (gotten from poolCount)
- */
- public abstract int poolID(String group, int pool);
-
- /** If the mod supports named pools, the names can be querried through this function.
+ /** If the mod supports named pools, the names can be queried through this function.
*
* @param group The TypeGroup the pool belongs to
* @param pool Array-like index for pool (gotten from poolCount)
*/
- public String poolName(String group, int pool) {
+ public String poolName(int pool) {
return "";
}
+
+ /** Returns true if the pool with the given id supports the given group
+ *
+ * @param pool The pool ID to query
+ * @param group The TypeGroup to query
+ * @return {@code true} if the pool supports the specified group, {@code false} otherwise
+ */
+ public abstract boolean poolProvides(int pool, String group);
/** If the mod supports pool descriptions, they can be accessed by this method.
*
* @param group The TypeGroup the pool belongs to
* @param pool Array-like index for pool (gotten from poolCount)
*/
- public String poolDescription(String group, int pool) {
+ public String poolDescription(int pool) {
return "";
}
}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java
index f3a6c47..3a57227 100644
--- a/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java
+++ b/src/main/java/net/cshift/api/transit/network/packet/DynamicPacket.java
@@ -3,14 +3,14 @@ package net.cshift.api.transit.network.packet;
import net.cshift.api.transit.type.Type;
/**
- * Simple packet which stores a fluid value.
+ * Simple packet which stores a value which can change.
*
+ * @param <D> The type parameter of the Type being transported
* @author Kyle Gunger
- *
- * @param <D> The data type (Object) that the packet transfers
+ *
*/
public class DynamicPacket<D> implements IDynamicPacket<D> {
- private D data;
+ private Object data;
private Type<D> type;
/** Constructor. Stores the given data and uses the given type.
@@ -18,20 +18,20 @@ public class DynamicPacket<D> implements IDynamicPacket<D> {
* @param dat The packet's data
* @param t The packet's type
*/
- public DynamicPacket(D dat, Type<D> t)
+ public DynamicPacket(Object dat, Type<D> t)
{
data = dat;
type = t;
}
@Override
- public D getData()
+ public Object getData()
{
return data;
}
@Override
- public void setData(D dat)
+ public void setData(Object dat)
{
data = dat;
}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java
index 334d5f6..6d80b3b 100644
--- a/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java
+++ b/src/main/java/net/cshift/api/transit/network/packet/IDynamicPacket.java
@@ -1,14 +1,11 @@
package net.cshift.api.transit.network.packet;
-/** Interface describing a fluid packet.
+/** Interface describing a packet where the data can change as it's transferred.
+ *
+ * @param <D> The type parameter of the Type being transported
* @author Kyle Gunger
- *
- * @param <D> The data type (Object) that the packet transfers
*/
public interface IDynamicPacket<D> extends IStaticPacket<D>{
- /**Set the packet's data.
- *
- * @return <D> The packet's data
- */
- public void setData(D dat);
+ /** Set the packet's data. */
+ public void setData(Object dat);
}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java b/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java
index ba1b17f..cdad90a 100644
--- a/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java
+++ b/src/main/java/net/cshift/api/transit/network/packet/IStaticPacket.java
@@ -3,21 +3,21 @@ package net.cshift.api.transit.network.packet;
import net.cshift.api.transit.type.*;
/** Interface describing an unchanging packet.
- * @author Kyle Gunger
*
- * @param <D> The data type (Object) that the packet transfers.
+ * @param <D> The type parameter of the Type being transported
+ * @author Kyle Gunger
*/
public interface IStaticPacket<D>
{
/**Get the packet's data.
*
- * @return <D> The packet's data
+ * @return The packet's data
*/
- public D getData();
+ public Object getData();
/**Get the packet's type.
*
- * @return IType<<D>> The type of the packet
+ * @return IType<?> The type of the packet
*/
public Type<D> getType();
}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java b/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java
deleted file mode 100644
index d382645..0000000
--- a/src/main/java/net/cshift/api/transit/network/packet/MetaDynamicPacket.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package net.cshift.api.transit.network.packet;
-
-import net.cshift.api.transit.type.Type;
-
-/** Static packet with extra data attached.
- * @author Kyle Gunger
- *
- * @param <D> The data type (Object) that the packet transfers
- * @param <M> The type of metadata
-*/
-public class MetaDynamicPacket<D, M> extends DynamicPacket<D>
-{
- private M metaData;
-
- /** Constructor
- *
- * @param dat The data to store
- * @param t The Type of the data
- * @param meta The metadata to store
- */
- public MetaDynamicPacket(D dat, Type<D> t, M meta)
- {
- super(dat, t);
- metaData = meta;
- }
-
- /** Get the metadata of the packet.
- *
- * @return The packet's metadata
- */
- public M getMetaData()
- {
- return metaData;
- }
-
- /** Set the metadata of the packet.
- *
- * @param meta The packet's new metadata
- */
- public void setMetaData(M meta)
- {
- metaData = meta;
- }
-}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java b/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java
deleted file mode 100644
index aea1b83..0000000
--- a/src/main/java/net/cshift/api/transit/network/packet/MetaPacket.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package net.cshift.api.transit.network.packet;
-
-import net.cshift.api.transit.type.*;
-
-/** Static packet with extra data attached.
- * @author Kyle Gunger
- *
- * @param <D> The data type (Object) that the packet transfers
- * @param <M> The type of metadata
-*/
-public class MetaPacket<D, M> extends StaticPacket<D>
-{
- private M metaData;
-
- /** Constructor
- *
- * @param dat The data to store
- * @param t The Type of the data
- * @param meta The metadata to store
- */
- public MetaPacket(D dat, Type<D> t, M meta)
- {
- super(dat, t);
- metaData = meta;
- }
-
- /** Get the metadata of the packet.
- *
- * @return The packet's metadata
- */
- public M getMetaData()
- {
- return metaData;
- }
-}
diff --git a/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java b/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java
index 2c6a16a..5ffed26 100644
--- a/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java
+++ b/src/main/java/net/cshift/api/transit/network/packet/StaticPacket.java
@@ -9,7 +9,7 @@ import net.cshift.api.transit.type.*;
*/
public class StaticPacket<D> implements IStaticPacket<D>
{
- private D data;
+ private Object data;
private Type<D> type;
/** Constructor. Stores the given data and uses the given type.
@@ -17,14 +17,14 @@ public class StaticPacket<D> implements IStaticPacket<D>
* @param dat The packet's data
* @param t The packet's type
*/
- public StaticPacket(D dat, Type<D> t)
+ public StaticPacket(Object dat, Type<D> t)
{
data = dat;
type = t;
}
@Override
- public D getData()
+ public Object getData()
{
return data;
}
diff --git a/src/main/java/net/cshift/api/transit/type/SimpleTypes.java b/src/main/java/net/cshift/api/transit/type/SimpleTypes.java
index 54106c7..5b4a6b8 100644
--- a/src/main/java/net/cshift/api/transit/type/SimpleTypes.java
+++ b/src/main/java/net/cshift/api/transit/type/SimpleTypes.java
@@ -27,17 +27,17 @@ package net.cshift.api.transit.type;
public final class SimpleTypes {
/** Transfers energy. Energy is stored as a numeric.
*/
- public static final Type<Long> TransitJoule = new Type<Long>("Joule", "ENERGY");
+ public static final Type<Long> TransitJoule = new Type<Long>("Joule", "ENERGY", Long.class);
/** Transfers mana. TMana stores mana count and type.
*/
- public static final Type<TMana> TransitMana = new Type<TMana>("TMana", "MANA");
+ public static final Type<TMana> TransitMana = new Type<TMana>("TMana", "MANA", TMana.class);
/** Transfers items. TItem stores an item and an item count.
*/
- public static final Type<TItem> TransitItem = new Type<TItem>("TItem", "ITEM");
+ public static final Type<TItem> TransitItem = new Type<TItem>("TItem", "ITEM", TItem.class);
/** Transfers fluid. TFluid stores fluid and mB.
*/
- public static final Type<TFluid> TransitFluid = new Type<TFluid>("TFluid", "FLUID");
+ public static final Type<TFluid> TransitFluid = new Type<TFluid>("TFluid", "FLUID", TFluid.class);
}
diff --git a/src/main/java/net/cshift/api/transit/type/Type.java b/src/main/java/net/cshift/api/transit/type/Type.java
index d47cd08..f544e8f 100644
--- a/src/main/java/net/cshift/api/transit/type/Type.java
+++ b/src/main/java/net/cshift/api/transit/type/Type.java
@@ -1,3 +1,27 @@
+/*
+ MIT License
+
+ Copyright (c) 2023 Kyle Gunger
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all
+ copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
package net.cshift.api.transit.type;
import net.cshift.api.transit.network.packet.*;
@@ -10,12 +34,14 @@ public class Type<T>
{
protected final String typeID;
protected final String groupID;
+ private final Class<?> underlying;
// Should not override, but you can if you must
- public Type(String tID, String gID)
+ public Type(String tID, String gID, Class<?> underlying)
{
typeID = tID;
groupID = gID;
+ this.underlying = underlying;
}
@@ -31,9 +57,13 @@ public class Type<T>
* @param group The group asking for the conversion
* @return The data in the group's base type
*/
- public T toBase(T data, String group)
+ @SuppressWarnings("unchecked")
+ public T toBase(Object data, String group)
{
- return data;
+ // Assume the underlying is the same as T for this very simple case
+ if (data.getClass().equals(this.getUnderlying()))
+ return (T) data;
+ return null;
}
/** Return the data formatted in the default group's base type.
@@ -41,7 +71,7 @@ public class Type<T>
* @param data The data (of this type)
* @return The data in the group's base type
*/
- public final T toBase(T data)
+ public final T toBase(Object data)
{
return this.toBase(data, this.groupID);
}
@@ -54,7 +84,9 @@ public class Type<T>
*/
public final T toBase(IStaticPacket<T> packet, String group)
{
- return this.toBase(packet.getData(), group);
+ if(packet.getType().equals(this))
+ return this.toBase(packet.getData(), group);
+ return null;
}
/** Return the packet's data formatted in the default group's base type.
@@ -78,19 +110,20 @@ public class Type<T>
*
* @param data The base data
* @param group The group asking for the conversion
- * @return The packet's data in the group's base type
+ * @return The converted data, in the Type's underlying
*/
- public T fromBase(T data, String group)
+ public Object fromBase(T data, String group)
{
+ // Assume T is the same as the underlying
return data;
}
/** Convert data from the default group's base type to this type.
*
* @param data The base data
- * @return The packet's data in the group's base type
+ * @return The converted data, in the Type's underlying
*/
- public final T fromBase(T data)
+ public final Object fromBase(T data)
{
return fromBase(data, this.groupID);
}
@@ -99,7 +132,7 @@ public class Type<T>
*
* @param data The base data
* @param group The group asking for the conversion
- * @return The packet's data in the group's base type
+ * @return A packet containing the converted data in the Type's underlying
*/
public final IStaticPacket<T> packetFromBase(T data, String group)
{
@@ -130,7 +163,7 @@ public class Type<T>
* @param data The data to convert
* @return The data formatted in this type
*/
- public T convertFrom(Type<T> from, T data, String group) {
+ public Object convertFrom(Type<T> from, Object data, String group) {
return this.fromBase(from.toBase(data, group), group);
}
@@ -140,7 +173,7 @@ public class Type<T>
* @param data The data to convert
* @return The data formatted in this type
*/
- public final T convertFrom(Type<T> from, T data) {
+ public final Object convertFrom(Type<T> from, Object data) {
return convertFrom(from, data, from.groupID);
}
@@ -159,7 +192,7 @@ public class Type<T>
* @param packet The packet to convert
* @return The new packet formatted in this type
*/
- public final IStaticPacket<T> convertFrom(IStaticPacket<T> packet) {
+ public final IStaticPacket<T> convertFrom(IStaticPacket<T> packet) throws ClassCastException {
return this.convertFrom(packet, packet.getType().groupID);
}
@@ -176,7 +209,7 @@ public class Type<T>
* @param data The data to convert
* @return The data formatted in this type
*/
- public final T convertTo(Type<T> to, T data, String group) {
+ public final Object convertTo(Type<T> to, Object data, String group) {
return to.convertFrom(this, data, group);
}
@@ -186,7 +219,7 @@ public class Type<T>
* @param data The data to convert
* @return The data formatted in this type
*/
- public final T convertTo(Type<T> to, T data) {
+ public final Object convertTo(Type<T> to, Object data) {
return to.convertFrom(this, data, this.groupID);
}
@@ -232,7 +265,16 @@ public class Type<T>
{
return groupID;
}
-
+
+ /** The underlying class which this Type uses to implement functionality within the mod
+ *
+ * @return Class<?>
+ */
+ public final Class<?> getUnderlying()
+ {
+ return underlying;
+ }
+
@Override
public final String toString() {
return groupID + ":" + typeID;
diff --git a/src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java b/src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java
index 5e09e71..f74e9c5 100644
--- a/src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java
+++ b/src/main/java/net/cshift/api/transit/type/group/GroupRegistry.java
@@ -1,3 +1,27 @@
+/*
+ MIT License
+
+ Copyright (c) 2023 Kyle Gunger
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all
+ copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
package net.cshift.api.transit.type.group;
import java.security.InvalidKeyException;
@@ -14,8 +38,7 @@ public final class GroupRegistry {
private GroupRegistry() {}
- public static final boolean addGroup(TypeGroup<?> group)
- {
+ public static final boolean addGroup(TypeGroup<?> group) {
if (GROUPS.containsKey(group.getGroup())) {
LOGGER.warn("Failed to add group " + group + " to the registry. Did another mod add a group with the same name?");
return false;
@@ -27,24 +50,67 @@ public final class GroupRegistry {
return true;
}
- public static final <T> TypeGroup<T> groupByID(String groupID)
- {
+ /** Get the requested type group via ID
+ *
+ * @param <B> The base type of the type group
+ * @param groupID
+ * @param baseClass
+ * @throws ClassCastException if the group is not of the provided class type
+ * @return The group (properly casted) if it exists, or null otherwise
+ */
+ @SuppressWarnings("unchecked")
+ public static final <B> TypeGroup<B> groupByID(String groupID, Class<B> baseClass) throws ClassCastException {
if (GROUPS.containsKey(groupID)) {
TypeGroup<?> g = GROUPS.get(groupID);
- return (TypeGroup<T>) g;
+ if (g.getBaseClass().equals(baseClass)) {
+ return (TypeGroup<B>) g;
+ } else {
+ throw new ClassCastException(
+ "Group " + g.getGroup() + " was requested as TypeGroup<" +
+ baseClass.getName() + ">, but the group is actually TypeGroup<" +
+ g.getBaseClass().getName() + ">"
+ );
+ }
}
return null;
}
- public static final <T> Type<T> typeByIdentity(String groupID, String typeID) throws InvalidKeyException
- {
- TypeGroup<T> group = GroupRegistry.<T>groupByID(groupID);
+ /** Get the wildcard generic TypeGroup via the groupID
+ *
+ * @param groupID The ID of the group to get
+ * @return The group if it exists, or null otherwise
+ */
+ public static final TypeGroup<?> groupByID(String groupID) {
+ if (GROUPS.containsKey(groupID)) {
+ return GROUPS.get(groupID);
+ }
- if(group != null)
- return group.getType(typeID);
-
return null;
}
+ /** Get the requested type via
+ *
+ * @param <B>
+ * @param groupID
+ * @param typeID
+ * @param baseClass
+ * @return
+ * @throws ClassCastException
+ */
+ public static final <B> Type<B> typeByIdentity(String groupID, String typeID, Class<B> baseClass) throws ClassCastException {
+ TypeGroup<B> group = GroupRegistry.<B>groupByID(groupID, baseClass);
+
+ if(group != null && group.isInGroup(typeID)) {
+ try {
+ return group.getType(typeID);
+ } catch (InvalidKeyException e) {
+ // Should never happen, since we just checked that the type is in the group
+ LOGGER.error("Unreachable in typeByIdentity: trying to get type '" + groupID + ":" + typeID + "'");
+ e.printStackTrace();
+ }
+ }
+
+ return null;
+ }
}
diff --git a/src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java b/src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java
index 66d0121..3f7d8a1 100644
--- a/src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java
+++ b/src/main/java/net/cshift/api/transit/type/group/SimpleGroups.java
@@ -31,19 +31,19 @@ import net.cshift.api.transit.type.*;
public final class SimpleGroups {
/** Transfers energy. Basic unit is TJoule (Transit Joule).
*/
- public static final TypeGroup<Long> ENERGY = new TypeGroup<Long>(SimpleTypes.TransitJoule);
+ public static final TypeGroup<Long> ENERGY = new TypeGroup<Long>(SimpleTypes.TransitJoule, Long.class);
/** Transfers mana. Basic unit is TMana (Transit Mana).
*/
- public static final TypeGroup<TMana> MANA = new TypeGroup<TMana>(SimpleTypes.TransitMana);
+ public static final TypeGroup<TMana> MANA = new TypeGroup<TMana>(SimpleTypes.TransitMana, TMana.class);
/** Transfers items. Basic unit is TItem.
*/
- public static final TypeGroup<TItem> ITEM = new TypeGroup<TItem>(SimpleTypes.TransitItem);
+ public static final TypeGroup<TItem> ITEM = new TypeGroup<TItem>(SimpleTypes.TransitItem, TItem.class);
/** Transfers fluids. Basic unit is TFluid.
*/
- public static final TypeGroup<TFluid> FLUID = new TypeGroup<TFluid>(SimpleTypes.TransitFluid);
+ public static final TypeGroup<TFluid> FLUID = new TypeGroup<TFluid>(SimpleTypes.TransitFluid, TFluid.class);
private static final Logger LOGGER = LoggerFactory.getLogger("Transit API/Simple Groups");
diff --git a/src/main/java/net/cshift/api/transit/type/group/TypeGroup.java b/src/main/java/net/cshift/api/transit/type/group/TypeGroup.java
index fcbb3ef..426f0a8 100644
--- a/src/main/java/net/cshift/api/transit/type/group/TypeGroup.java
+++ b/src/main/java/net/cshift/api/transit/type/group/TypeGroup.java
@@ -25,7 +25,6 @@
package net.cshift.api.transit.type.group;
import java.security.InvalidKeyException;
-import java.sql.Types;
import java.util.HashMap;
import org.slf4j.*;
@@ -38,6 +37,9 @@ import net.cshift.api.transit.type.Type;
*/
public final class TypeGroup<B>
{
+ // The basic class of the base type which is being transferred
+ private Class<B> baseClass;
+
// The base Type (provides the group's identifier)
private Type<B> baseType;
@@ -48,10 +50,11 @@ public final class TypeGroup<B>
private static final Logger LOGGER = LoggerFactory.getLogger("Transit API/Type Group");
- public TypeGroup(Type<B> base)
+ public TypeGroup(Type<B> base, Class<B> baseClass)
{
TYPES = new HashMap<String, Type<B>>();
baseType = base;
+ this.baseClass = baseClass;
addType(base);
}
@@ -207,11 +210,18 @@ public final class TypeGroup<B>
}
/** The base group type
- * @return Type
+ * @return Type<B>
*/
public final Type<B> getBase() {
return baseType;
}
+
+ /** A Class object which represents the type that the base type uses to transfer data
+ * @return Class<B>
+ */
+ public final Class<B> getBaseClass() {
+ return baseClass;
+ }
@Override
public final String toString() {