fixed formatting

define_list_fix
Sebastian Weiß 9 years ago
parent e8f76d43fe
commit dccec876c5
  1. 1229
      jme3-lwjgl3/src/main/java/com/jme3/lwjgl3/utils/APIBuffer.java
  2. 372
      jme3-lwjgl3/src/main/java/com/jme3/lwjgl3/utils/APIUtil.java

@ -4,12 +4,6 @@
*/ */
package com.jme3.lwjgl3.utils; package com.jme3.lwjgl3.utils;
import com.jme3.lwjgl3.utils.APIBuffer;
import org.lwjgl.system.linux.LinuxLibrary;
import org.lwjgl.system.macosx.MacOSXLibrary;
import org.lwjgl.system.windows.WindowsLibrary;
import java.io.PrintStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.HashMap; import java.util.HashMap;
@ -20,173 +14,205 @@ import java.util.regex.Pattern;
/** /**
* Utility class useful to API bindings. [INTERNAL USE ONLY] * Utility class useful to API bindings. [INTERNAL USE ONLY]
* *
* <p>Method names in this class are prefixed with {@code api} to avoid ambiguities when used with static imports.</p> * <p>
* Method names in this class are prefixed with {@code api} to avoid ambiguities
* when used with static imports.</p>
*/ */
public final class APIUtil { public final class APIUtil {
private static final ThreadLocal<APIBuffer> API_BUFFERS = new ThreadLocal<APIBuffer>() { private static final ThreadLocal<APIBuffer> API_BUFFERS = new ThreadLocal<APIBuffer>() {
@Override @Override
protected APIBuffer initialValue() { protected APIBuffer initialValue() {
return new APIBuffer(); return new APIBuffer();
} }
}; };
private APIUtil() { private APIUtil() {
} }
/**
/** Returns a thread-local {@link APIBuffer} that has been reset. */ * Returns a thread-local {@link APIBuffer} that has been reset.
public static APIBuffer apiBuffer() { */
return API_BUFFERS.get().reset(); public static APIBuffer apiBuffer() {
} return API_BUFFERS.get().reset();
}
/**
* Returns a thread-local {@link APIBuffer}, without resetting it. This makes the APIBuffer work like a stack when used in nested API calls. The user is /**
* responsible for resetting the {@link APIBuffer} to an appropriate state before the nested call returns. * Returns a thread-local {@link APIBuffer}, without resetting it. This
* * makes the APIBuffer work like a stack when used in nested API calls. The
* @see APIBuffer#pop * user is responsible for resetting the {@link APIBuffer} to an appropriate
*/ * state before the nested call returns.
public static APIBuffer apiStack() { *
return API_BUFFERS.get().push(); * @see APIBuffer#pop
} */
public static APIBuffer apiStack() {
/** A data class for API versioning information. */ return API_BUFFERS.get().push();
public static class APIVersion { }
/** Returns the API major version. */ /**
public final int major; * A data class for API versioning information.
/** Returns the API minor version. */ */
public final int minor; public static class APIVersion {
/** Returns the API revision. May be null. */ /**
public final String revision; * Returns the API major version.
/** Returns the API implementation-specific versioning information. May be null. */ */
public final String implementation; public final int major;
/**
public APIVersion(int major, int minor) { * Returns the API minor version.
this(major, minor, null, null); */
} public final int minor;
public APIVersion(int major, int minor, String revision, String implementation) { /**
this.major = major; * Returns the API revision. May be null.
this.minor = minor; */
this.revision = revision; public final String revision;
this.implementation = implementation; /**
} * Returns the API implementation-specific versioning information. May
* be null.
} */
public final String implementation;
/**
* Parses a version string. The version string must have the format {@code MAJOR.MINOR.REVISION IMPL}, where {@code MAJOR} is the major version (integer), public APIVersion(int major, int minor) {
* {@code MINOR} is the minor version (integer), {@code REVISION} is the revision version (string, optional) and {@code IMPL} is implementation-specific this(major, minor, null, null);
* information (string, optional). }
*
* @param version the API version string public APIVersion(int major, int minor, String revision, String implementation) {
* this.major = major;
* @return the parsed {@link APIVersion} this.minor = minor;
*/ this.revision = revision;
public static APIVersion apiParseVersion(String version) { this.implementation = implementation;
return apiParseVersion(version, null); }
}
}
/**
* Parses a version string. The version string must have the format {@code PREFIX MAJOR.MINOR.REVISION IMPL}, where {@code PREFIX} is the specified prefix /**
* (string, optional), {@code MAJOR} is the major version (integer), {@code MINOR} is the minor version (integer), {@code REVISION} is the revision version * Parses a version string. The version string must have the format
* (string, optional) and {@code IMPL} is implementation-specific information (string, optional). * {@code MAJOR.MINOR.REVISION IMPL}, where {@code MAJOR} is the major
* * version (integer), {@code MINOR} is the minor version (integer),
* @param version the version string * {@code REVISION} is the revision version (string, optional) and
* @param prefix the version string prefix, may be null * {@code IMPL} is implementation-specific information (string, optional).
* *
* @return the parsed {@link APIVersion} * @param version the API version string
*/ *
public static APIVersion apiParseVersion(String version, String prefix) { * @return the parsed {@link APIVersion}
String pattern = "([0-9]+)[.]([0-9]+)([.]\\S+)?\\s*(.+)?"; */
if ( prefix != null ) public static APIVersion apiParseVersion(String version) {
pattern = prefix + "\\s+" + pattern; return apiParseVersion(version, null);
}
Matcher matcher = Pattern.compile(pattern).matcher(version);
if ( !matcher.matches() ) /**
throw new IllegalArgumentException(String.format("Malformed API version string [%s]", version)); * Parses a version string. The version string must have the format
* {@code PREFIX MAJOR.MINOR.REVISION IMPL}, where {@code PREFIX} is the
return new APIVersion( * specified prefix (string, optional), {@code MAJOR} is the major version
Integer.parseInt(matcher.group(1)), * (integer), {@code MINOR} is the minor version (integer), {@code REVISION}
Integer.parseInt(matcher.group(2)), * is the revision version (string, optional) and {@code IMPL} is
matcher.group(3), * implementation-specific information (string, optional).
matcher.group(4) *
); * @param version the version string
} * @param prefix the version string prefix, may be null
*
public static String apiUnknownToken(int token) { * @return the parsed {@link APIVersion}
return apiUnknownToken("Unknown", token); */
} public static APIVersion apiParseVersion(String version, String prefix) {
String pattern = "([0-9]+)[.]([0-9]+)([.]\\S+)?\\s*(.+)?";
public static String apiUnknownToken(String description, int token) { if (prefix != null) {
return String.format("%s [0x%X]", description, token); pattern = prefix + "\\s+" + pattern;
} }
/** Matcher matcher = Pattern.compile(pattern).matcher(version);
* Returns a map of public static final integer fields in the specified classes, to their String representations. An optional filter can be specified to if (!matcher.matches()) {
* only include specific fields. The target map may be null, in which case a new map is allocated and returned. throw new IllegalArgumentException(String.format("Malformed API version string [%s]", version));
* }
* <p>This method is useful when debugging to quickly identify values returned from an API.</p>
* return new APIVersion(
* @param filter the filter to use (optional) Integer.parseInt(matcher.group(1)),
* @param target the target map (optional) Integer.parseInt(matcher.group(2)),
* @param tokenClasses the classes to get tokens from matcher.group(3),
* matcher.group(4)
* @return the token map );
*/ }
public static Map<Integer, String> apiClassTokens(TokenFilter filter, Map<Integer, String> target, Class<?>... tokenClasses) {
if ( target == null ) public static String apiUnknownToken(int token) {
target = new HashMap<Integer, String>(64); return apiUnknownToken("Unknown", token);
}
int TOKEN_MODIFIERS = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
public static String apiUnknownToken(String description, int token) {
for ( Class<?> tokenClass : tokenClasses ) { return String.format("%s [0x%X]", description, token);
if ( tokenClass == null ) }
continue;
/**
for ( Field field : tokenClass.getDeclaredFields() ) { * Returns a map of public static final integer fields in the specified
// Get only <public static final int> fields. * classes, to their String representations. An optional filter can be
if ( (field.getModifiers() & TOKEN_MODIFIERS) == TOKEN_MODIFIERS && field.getType() == int.class ) { * specified to only include specific fields. The target map may be null, in
try { * which case a new map is allocated and returned.
int value = field.getInt(null); *
if ( filter != null && !filter.accept(field, value) ) * <p>
continue; * This method is useful when debugging to quickly identify values returned
* from an API.</p>
String name = target.get(value); *
target.put(value, name == null ? field.getName() : name + "|" + field.getName()); * @param filter the filter to use (optional)
} catch (IllegalAccessException e) { * @param target the target map (optional)
// Ignore * @param tokenClasses the classes to get tokens from
} *
} * @return the token map
} */
} public static Map<Integer, String> apiClassTokens(TokenFilter filter, Map<Integer, String> target, Class<?>... tokenClasses) {
if (target == null) {
return target; target = new HashMap<Integer, String>(64);
} }
public static Class<?> apiOptionalClass(String className) { int TOKEN_MODIFIERS = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
try {
return Class.forName(className); for (Class<?> tokenClass : tokenClasses) {
} catch (ClassNotFoundException e) { if (tokenClass == null) {
return null; continue;
} }
}
for (Field field : tokenClass.getDeclaredFields()) {
/** Simple interface for Field filtering. */ // Get only <public static final int> fields.
public interface TokenFilter { if ((field.getModifiers() & TOKEN_MODIFIERS) == TOKEN_MODIFIERS && field.getType() == int.class) {
try {
/** int value = field.getInt(null);
* Should return true if the specified Field passes the filter. if (filter != null && !filter.accept(field, value)) {
* continue;
* @param field the Field to test }
* @param value the integer value of the field
* String name = target.get(value);
* @return true if the Field is accepted target.put(value, name == null ? field.getName() : name + "|" + field.getName());
*/ } catch (IllegalAccessException e) {
boolean accept(Field field, int value); // Ignore
}
} }
}
} }
return target;
}
public static Class<?> apiOptionalClass(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return null;
}
}
/**
* Simple interface for Field filtering.
*/
public interface TokenFilter {
/**
* Should return true if the specified Field passes the filter.
*
* @param field the Field to test
* @param value the integer value of the field
*
* @return true if the Field is accepted
*/
boolean accept(Field field, int value);
}
}

Loading…
Cancel
Save