Skip to content

Commit

Permalink
updated javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Jan 15, 2024
1 parent 8352e76 commit 4341452
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 117 deletions.
13 changes: 8 additions & 5 deletions src/main/java/org/myrobotlab/codec/CodecUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@
* /api/returnEncoding/inputEncoding/service/method/param1/param2/ ...
* <p>
* xmpp for example assumes (/api/string/json)/service/method/param1/param2/ ...
* </p>
* <p>
* scheme = alpha *( alpha | digit | "+" | "-" | "." ) Components of all URIs: [
* &lt;scheme&gt;:]&lt;scheme-specific-part&gt;[#&lt;fragment&gt;]
* </p>
* <p>
* branch API test 5
*
* </p>
*
* @see <a href=
* "http://stackoverflow.com/questions/3641722/valid-characters-for-uri-schemes">Valid
* characters for URI schemes</a>
Expand Down Expand Up @@ -126,17 +129,17 @@ public class CodecUtils {
public static final String API_SERVICE = "service";

/**
* The path from a top-level URL to the messages API endpoint.
* <p>
* The path from a top-level URL to the messages API endpoint.
* </p>
* FIXME This should be moved to WebGui, CodecUtils should have no knowledge
* of URLs
*/
public static final String API_MESSAGES_PATH = PARAMETER_API + API_MESSAGES;

/**
* The path from a top-level URL to the service API endpoint.
* <p>
* The path from a top-level URL to the service API endpoint.
* </p>
* FIXME This should be moved to WebGui, CodecUtils should have no knowledge
* of URLs
Expand Down Expand Up @@ -1157,8 +1160,8 @@ static public Message pathToMsg(String from, String path) {
*
* It will return the json parts in a string array
*
* @param input
* @return
* @param input - json input
* @return - object array of params
*/
public static Object[] extractJsonParamsFromPath(String input) {
List<Object> fromJson = new ArrayList<>();
Expand Down
219 changes: 109 additions & 110 deletions src/main/java/org/myrobotlab/framework/StaticType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,135 +6,134 @@
import java.util.Objects;

/**
* This class acts as a container for generic
* type information that is retained at runtime.
* At first glance that statement would seem to
* violate the most important element of Java
* generics: type erasure. However, Java does
* in fact reify generic types in extremely
* specific circumstances. One of those circumstances
* is when a subclass is created that statically
* provides concrete type parameters. For example:
* <p></p>
* {@code
* new StaticType<String>(){}
* }
* <p></p>
* The following does not work because it does not provide
* a static concrete type parameter:
* <p></p>
* {@code
* new StaticType<T>(){}
* }
* This class acts as a container for generic type information that is retained
* at runtime. At first glance that statement would seem to violate the most
* important element of Java generics: type erasure. However, Java does in fact
* reify generic types in extremely specific circumstances. One of those
* circumstances is when a subclass is created that statically provides concrete
* type parameters. For example:
* <p>
* The above will throw an {@link IllegalArgumentException}.
* {@code new StaticType<String>(){} }
* </p>
* <p>
* This concept comes from a 2006 blog post from Neal Gafter.
* <a href="http://gafter.blogspot.com/2006/12/super-type-tokens.html?showComment=1171980720000#c2954512480577635806">
* A comment</a> on that blog post suggested implementing a generic interface with
* a method signature containing the generic type parameter, such as
* {@link Comparable}. This should have made it a syntax error
* to use raw types, but I was unable to cause such an error to occur.
* Thus, this class instead checks for raw types in the constructor
* by checking whether {@link Class#getGenericSuperclass()} returns
* an instance of {@link ParameterizedType} or not. If not,
* {@link IllegalArgumentException} is thrown.
* The following does not work because it does not provide a static concrete
* type parameter:
* </p>
* {@code new StaticType<T>(){} }
* <p>
* The above will throw an {@link IllegalArgumentException}.
* </p>
* <p>
* This concept comes from a 2006 blog post from Neal Gafter. <a href=
* "http://gafter.blogspot.com/2006/12/super-type-tokens.html?showComment=1171980720000#c2954512480577635806">
* A comment</a> on that blog post suggested implementing a generic interface
* with a method signature containing the generic type parameter, such as
* {@link Comparable}. This should have made it a syntax error to use raw types,
* but I was unable to cause such an error to occur. Thus, this class instead
* checks for raw types in the constructor by checking whether
* {@link Class#getGenericSuperclass()} returns an instance of
* {@link ParameterizedType} or not. If not, {@link IllegalArgumentException} is
* thrown.
* </p>
*
* @param <T> The concrete type that this StaticType contains
* @see <a href="http://gafter.blogspot.com/2006/12/super-type-tokens.html">Super Type Tokens</a>
* @param <T>
* The concrete type that this StaticType contains
* @see <a href=
* "http://gafter.blogspot.com/2006/12/super-type-tokens.html">Super Type
* Tokens</a>
* @author AutonomicPerfectionist
*/
public abstract class StaticType<T> {
private final Type storedType;
private final Type storedType;

/**
* Constructs this StaticType object
* and ensures the generic type parameters
* were provided and are concrete.
*
* @throws IllegalArgumentException if the generic
* type parameters were not provided or were not concrete.
*/
protected StaticType() {
// This gets the type passed into the T
// just like in the source code. This means that
// if you do new StaticType<T>(){}
// this genericType *will not* have the type of T
Type genericType = getClass().getGenericSuperclass();
if (!(genericType instanceof ParameterizedType))
throw new IllegalArgumentException("StaticType must not be a raw type");
storedType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
validateType(storedType);
/**
* Constructs this StaticType object and ensures the generic type parameters
* were provided and are concrete.
*
* @throws IllegalArgumentException
* if the generic type parameters were not provided or were not
* concrete.
*/
protected StaticType() {
// This gets the type passed into the T
// just like in the source code. This means that
// if you do new StaticType<T>(){}
// this genericType *will not* have the type of T
Type genericType = getClass().getGenericSuperclass();
if (!(genericType instanceof ParameterizedType))
throw new IllegalArgumentException("StaticType must not be a raw type");
storedType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
validateType(storedType);

}
}

/**
* Gets the stored {@link Type}
* instance. This type should contain the type of
* {@link T}, including concrete generic type
* parameters if T is a generic type itself.
* @return The stored type
*/
public Type getType() {
return storedType;
}
/**
* Gets the stored {@link Type} instance. This type should contain the type of
* {@link T}, including concrete generic type parameters if T is a generic
* type itself.
*
* @return The stored type
*/
public Type getType() {
return storedType;
}

/**
* Get the stored type as a Class object
* that can be used for checking cast
* compatibility. Note that the resulting
* Class object will not check for generic
* compatibility. If the stored type
* is not a Class type, then this method
* throws {@link IllegalStateException}.
*
* @return The internal stored type cast to a Class object
* @throws IllegalStateException if the stored type is not a Class.
*/
@SuppressWarnings("unchecked")
public Class<T> asClass() {
if (storedType instanceof Class) {
return (Class<T>) storedType;
} else {
throw new IllegalStateException("Stored type " + storedType + " is not a Class.");
}
/**
* Get the stored type as a Class object that can be used for checking cast
* compatibility. Note that the resulting Class object will not check for
* generic compatibility. If the stored type is not a Class type, then this
* method throws {@link IllegalStateException}.
*
* @return The internal stored type cast to a Class object
* @throws IllegalStateException
* if the stored type is not a Class.
*/
@SuppressWarnings("unchecked")
public Class<T> asClass() {
if (storedType instanceof Class) {
return (Class<T>) storedType;
} else {
throw new IllegalStateException("Stored type " + storedType + " is not a Class.");
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StaticType)) return false;
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof StaticType))
return false;

StaticType<?> that = (StaticType<?>) o;
StaticType<?> that = (StaticType<?>) o;

return Objects.equals(storedType, that.storedType);
}
return Objects.equals(storedType, that.storedType);
}

@Override
public int hashCode() {
return storedType != null ? storedType.hashCode() : 0;
}
@Override
public int hashCode() {
return storedType != null ? storedType.hashCode() : 0;
}

@Override
public String toString() {
return storedType.toString();
}

@Override
public String toString() {
return storedType.toString();
/**
* Function to recursively validate type parameters to ensure they are all
* concrete so no type variables sneak in.
*
* @param type
* The type to check
*/
private static void validateType(Type type) {
if (type instanceof ParameterizedType) {
for (Type param : ((ParameterizedType) type).getActualTypeArguments()) {
validateType(param);
}
} else if (type instanceof TypeVariable) {
throw new IllegalArgumentException("Cannot construct a StaticType with any non-concrete type variables");
}
/**
* Function to recursively validate type parameters
* to ensure they are all concrete so no type variables sneak in.
* @param type The type to check
*/
private static void validateType(Type type) {
if (type instanceof ParameterizedType) {
for (Type param : ((ParameterizedType) type).getActualTypeArguments()) {
validateType(param);
}
} else if (type instanceof TypeVariable) {
throw new IllegalArgumentException("Cannot construct a StaticType with any non-concrete type variables");
}

}
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/myrobotlab/service/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -5328,7 +5328,6 @@ public ServiceConfig getPeerConfig(String serviceName, String peerKey) {
* @param serviceName
* @param type
* @return
* @throws IOException
*/
public boolean changeType(String serviceName, String type) {
try {
Expand Down Expand Up @@ -5376,7 +5375,7 @@ public ServiceConfig getPeer(String sericeName, String peerKey) {
/**
* Removes a config set and all its files
*
* @param string
* @param configName - name of config
*/
public static void removeConfig(String configName) {
try {
Expand Down

0 comments on commit 4341452

Please sign in to comment.