You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2013/09/06 14:53:35 UTC
[6/8] ISIS-509: tidy-up of Util classes.
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassExtensions.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassExtensions.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassExtensions.java
new file mode 100644
index 0000000..58eceae
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassExtensions.java
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import com.google.common.io.InputSupplier;
+import com.google.common.io.Resources;
+
+import org.apache.isis.core.commons.exceptions.IsisException;
+import org.apache.isis.core.metamodel.methodutils.MethodFinderUtils;
+
+public final class ClassExtensions {
+
+
+
+
+ // //////////////////////////////////////
+
+
+ private ClassExtensions() {
+ }
+
+ public static Object newInstance(final Class<?> extendee, final Class<?> constructorParamType, final Object constructorArg) {
+ return ClassExtensions.newInstance(extendee, new Class[] { constructorParamType }, new Object[] { constructorArg });
+ }
+
+ /**
+ * Tries to instantiate using a constructor accepting the supplied
+ * arguments; if no such constructor then falls back to trying the no-arg
+ * constructor.
+ */
+ public static Object newInstance(final Class<?> extendee, final Class<?>[] constructorParamTypes, final Object[] constructorArgs) {
+ try {
+ Constructor<?> constructor;
+ try {
+ constructor = extendee.getConstructor(constructorParamTypes);
+ return constructor.newInstance(constructorArgs);
+ } catch (final NoSuchMethodException ex) {
+ try {
+ constructor = extendee.getConstructor();
+ return constructor.newInstance();
+ } catch (final NoSuchMethodException e) {
+ throw new IsisException(e);
+ }
+ }
+ } catch (final SecurityException ex) {
+ throw new IsisException(ex);
+ } catch (final IllegalArgumentException e) {
+ throw new IsisException(e);
+ } catch (final InstantiationException e) {
+ throw new IsisException(e);
+ } catch (final IllegalAccessException e) {
+ throw new IsisException(e);
+ } catch (final InvocationTargetException e) {
+ throw new IsisException(e);
+ }
+ }
+
+ public static String getSuperclass(final Class<?> extendee) {
+ final Class<?> superType = extendee.getSuperclass();
+
+ if (superType == null) {
+ return null;
+ }
+ return superType.getName();
+ }
+
+ public static boolean isAbstract(final Class<?> extendee) {
+ return Modifier.isAbstract(extendee.getModifiers());
+ }
+
+ public static boolean isFinal(final Class<?> extendee) {
+ return Modifier.isFinal(extendee.getModifiers());
+ }
+
+ public static boolean isPublic(final Class<?> extendee) {
+ return Modifier.isPublic(extendee.getModifiers());
+ }
+
+ public static boolean isJavaClass(final Class<?> extendee) {
+ final String className = extendee.getName();
+ return className.startsWith(ClassUtil.JAVA_CLASS_PREFIX) ||
+ extendee.getName().startsWith("sun.");
+ }
+
+ static Class<?> implementingClassOrNull(final Class<?> extendee, final Class<?> requiredClass, final Class<?> constructorParamType) {
+ if (extendee == null) {
+ return null;
+ }
+ if (!requiredClass.isAssignableFrom(extendee)) {
+ return null;
+ }
+ try {
+ extendee.getConstructor(new Class[] { constructorParamType });
+ } catch (final NoSuchMethodException ex) {
+ try {
+ extendee.getConstructor(new Class[] {});
+ } catch (final NoSuchMethodException e) {
+ return null;
+ }
+ } catch (final SecurityException e) {
+ return null;
+ }
+ final int modifiers = extendee.getModifiers();
+ if (!Modifier.isPublic(modifiers)) {
+ return null;
+ }
+ return extendee;
+ }
+
+ public static Method getMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterClass) throws NoSuchMethodException {
+ return clazz.getMethod(methodName, parameterClass);
+ }
+
+ public static Method getMethodElseNull(final Class<?> clazz, final String methodName, final Class<?>... parameterClass) {
+ try {
+ return clazz.getMethod(methodName, parameterClass);
+ } catch (final NoSuchMethodException e) {
+ return null;
+ }
+ }
+
+ public static Method findMethodElseNull(final Class<?> clazz, final String[] candidateMethodNames, final Class<?>... parameterClass) {
+ for (final String candidateMethodName : candidateMethodNames) {
+ final Method method = getMethodElseNull(clazz, candidateMethodName, parameterClass);
+ if (method != null) {
+ return method;
+ }
+ }
+ return null;
+ }
+
+ public static Properties resourceProperties(final Class<?> extendee, final String suffix) {
+ try {
+ final URL url = Resources.getResource(extendee, extendee.getSimpleName()+suffix);
+ final InputSupplier<InputStream> inputSupplier = com.google.common.io.Resources.newInputStreamSupplier(url);
+ final Properties properties = new Properties();
+ properties.load(inputSupplier.getInput());
+ return properties;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ public static String resourceContent(final Class<?> cls, final String suffix) throws IOException {
+ final URL url = Resources.getResource(cls, cls.getSimpleName()+suffix);
+ return Resources.toString(url, Charset.defaultCharset());
+ }
+
+ public static Class<?> asWrapped(final Class<?> primitiveClassExtendee) {
+ return ClassUtil.wrapperClasses.get(primitiveClassExtendee);
+ }
+
+ public static Class<? extends Object> asWrappedIfNecessary(final Class<?> cls) {
+ return cls.isPrimitive() ? asWrapped(cls) : cls;
+ }
+
+ public static Object toDefault(final Class<?> extendee) {
+ if(!extendee.isPrimitive()) {
+ return null;
+ }
+ return ClassUtil.defaultByPrimitiveClass.get(extendee);
+ }
+
+ /**
+ * Returns the corresponding 'null' value for the primitives, or just
+ * <tt>null</tt> if the class represents a non-primitive type.
+ */
+ public static Object getNullOrDefault(final Class<?> type) {
+ return ClassUtil.defaultByPrimitiveType.get(type);
+ }
+
+ public static boolean isCompatibleAsReturnType(final Class<?> returnTypeExtendee, final boolean canBeVoid, final Class<?> type) {
+ if (returnTypeExtendee == null) {
+ return true;
+ }
+ if (canBeVoid && (type == void.class)) {
+ return true;
+ }
+
+ if (type.isPrimitive()) {
+ return returnTypeExtendee.isAssignableFrom(ClassUtil.wrapperClasses.get(type));
+ }
+
+ return (returnTypeExtendee.isAssignableFrom(type));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassFunctions.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassFunctions.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassFunctions.java
new file mode 100644
index 0000000..3fd8999
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassFunctions.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.core.commons.lang;
+
+import com.google.common.base.Function;
+
+public final class ClassFunctions {
+
+ private ClassFunctions(){}
+
+ public static <T> Function<Object, T> castTo(final Class<T> type) {
+ return new Function<Object, T>() {
+ @SuppressWarnings("unchecked")
+ @Override
+ public T apply(final Object input) {
+ return (T) input;
+ }
+ };
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassMatchers.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassMatchers.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassMatchers.java
new file mode 100644
index 0000000..8491e69
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassMatchers.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+
+public final class ClassMatchers {
+
+ public static Matcher<?> isSubclassOf(final Class<?> superClass) {
+ return new BaseMatcher<Object>() {
+ @Override
+ public boolean matches(final Object item) {
+ final Class<?> cls = (Class<?>) item;
+ return superClass.isAssignableFrom(cls);
+ }
+
+ @Override
+ public void describeTo(final Description description) {
+ description.appendText("is subclass of " + superClass.getName());
+ }
+ };
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassPredicates.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassPredicates.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassPredicates.java
new file mode 100644
index 0000000..803265b
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassPredicates.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.core.commons.lang;
+
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+
+public final class ClassPredicates {
+
+ private ClassPredicates(){}
+
+ public static final <T> Predicate<Object> isOfType(final Class<T> type) {
+ return new Predicate<Object>() {
+ @Override
+ public boolean apply(Object input) {
+ return type.isAssignableFrom(input.getClass());
+ }
+ };
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassUtil.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassUtil.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassUtil.java
index 3dc791d..fc0d72e 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassUtil.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ClassUtil.java
@@ -19,49 +19,159 @@
package org.apache.isis.core.commons.lang;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
-import org.apache.isis.core.commons.exceptions.IsisException;
+import com.google.common.collect.Maps;
public final class ClassUtil {
+ static final String JAVA_CLASS_PREFIX = "java.";
+
+ private static Map<String, Class<?>> builtInClasses = new HashMap<String, Class<?>>();
+
+ static {
+ put(void.class);
+ put(boolean.class);
+ put(char.class);
+ put(byte.class);
+ put(short.class);
+ put(int.class);
+ put(long.class);
+ put(float.class);
+ put(double.class);
+ }
+
+ private static void put(final Class<?> cls) {
+ builtInClasses.put(cls.getName(), cls);
+ }
+
+ static final Map<Class<?>, Object> defaultByPrimitiveClass =
+ MapUtil.asMap(
+ boolean.class, false,
+ byte.class, (byte)0,
+ short.class, (short)0,
+ int.class, 0,
+ long.class, 0L,
+ float.class, 0.0f,
+ double.class, 0.0,
+ char.class, (char)0
+ );
+ static Map<Class<?>, Class<?>> wrapperClasses =
+ MapUtil.asMap(
+ // TODO: there is a better way of doing this in 1.6 using TypeMirror
+ boolean.class, Boolean.class,
+ byte.class, Byte.class,
+ char.class, Character.class,
+ short.class, Short.class,
+ int.class, Integer.class,
+ long.class, Long.class,
+ float.class, Float.class,
+ double.class, Double.class,
+ void.class, Void.class
+ );
+
+ static Map<Class<?>, Object> defaultByPrimitiveType = new HashMap<Class<?>, Object>();
+
+ static {
+ defaultByPrimitiveType.put(byte.class, (byte) 0);
+ defaultByPrimitiveType.put(short.class, (short) 0);
+ defaultByPrimitiveType.put(int.class, 0);
+ defaultByPrimitiveType.put(long.class, 0L);
+ defaultByPrimitiveType.put(char.class, 0);
+ defaultByPrimitiveType.put(float.class, 0.0F);
+ defaultByPrimitiveType.put(double.class, 0.0);
+ defaultByPrimitiveType.put(boolean.class, false);
+ }
+
+ public static Map<String, Class<?>> primitives = Maps.newHashMap();
+
+ static {
+ @SuppressWarnings({ "rawtypes" })
+ final List<Class> primitiveClasses = Arrays.<Class> asList(
+ boolean.class,
+ byte.class,
+ short.class,
+ int.class,
+ long.class,
+ float.class,
+ double.class,
+ char.class);
+ for (final Class<?> cls : primitiveClasses) {
+ primitives.put(cls.getName(), cls);
+ }
+ }
+
+
+ // //////////////////////////////////////
+
+
+
private ClassUtil() {
}
- public static Object newInstance(final Class<?> type, final Class<?> constructorParamType, final Object constructorArg) {
- return ClassUtil.newInstance(type, new Class[] { constructorParamType }, new Object[] { constructorArg });
+ public static Class<?> getBuiltIn(final String name) {
+ return builtInClasses.get(name);
}
/**
- * Tries to instantiate using a constructor accepting the supplied
- * arguments; if no such constructor then falls back to trying the no-arg
- * constructor.
+ * Returns the supplied Class so long as it implements (or is a subclass of)
+ * the required class, and also has either a constructor accepting the
+ * specified param type, or has a no-arg constructor.
*/
- public static Object newInstance(final Class<?> type, final Class<?>[] constructorParamTypes, final Object[] constructorArgs) {
+ public static Class<?> implementingClassOrNull(final String classCandidateName, final Class<?> requiredClass, final Class<?> constructorParamType) {
+ if (classCandidateName == null) {
+ return null;
+ }
+ Class<?> classCandidate = null;
try {
- Constructor<?> constructor;
- try {
- constructor = type.getConstructor(constructorParamTypes);
- return constructor.newInstance(constructorArgs);
- } catch (final NoSuchMethodException ex) {
- try {
- constructor = type.getConstructor();
- return constructor.newInstance();
- } catch (final NoSuchMethodException e) {
- throw new IsisException(e);
- }
+ classCandidate = Class.forName(classCandidateName);
+ return ClassExtensions.implementingClassOrNull(classCandidate, requiredClass, constructorParamType);
+ } catch (final ClassNotFoundException e) {
+ return null;
+ }
+ }
+
+ public static boolean directlyImplements(final Class<?> cls, final Class<?> interfaceType) {
+ for (final Class<?> directlyImplementedInterface : cls.getInterfaces()) {
+ if (directlyImplementedInterface == interfaceType) {
+ return true;
}
- } catch (final SecurityException ex) {
- throw new IsisException(ex);
- } catch (final IllegalArgumentException e) {
- throw new IsisException(e);
- } catch (final InstantiationException e) {
- throw new IsisException(e);
- } catch (final IllegalAccessException e) {
- throw new IsisException(e);
- } catch (final InvocationTargetException e) {
- throw new IsisException(e);
+ }
+ return false;
+ }
+
+ public static boolean directlyImplements(final Class<?> extendee, final String interfaceTypeName) {
+ try {
+ final Class<?> interfaceType = Thread.currentThread().getContextClassLoader().loadClass(interfaceTypeName);
+ return directlyImplements(extendee, interfaceType);
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static Class<?> forName(final String fullName) {
+ final Class<?> primitiveCls = primitives.get(fullName);
+ if (primitiveCls != null) {
+ return primitiveCls;
+ }
+ try {
+ return Thread.currentThread().getContextClassLoader().loadClass(fullName);
+ } catch (final ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static Class<?> forNameElseNull(final String fullName) {
+ if (fullName == null) {
+ return null;
+ }
+ try {
+ return Thread.currentThread().getContextClassLoader().loadClass(fullName);
+ } catch (final ClassNotFoundException e) {
+ return null;
}
}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/CloseableExtensions.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/CloseableExtensions.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/CloseableExtensions.java
new file mode 100644
index 0000000..77a606b
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/CloseableExtensions.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.io.Closeable;
+
+public final class CloseableExtensions {
+
+ private CloseableExtensions() {
+ }
+
+ public static void closeSafely(final Closeable extendee) {
+ if (extendee != null) {
+ try {
+ extendee.close();
+ } catch (final Exception ignore) {
+ // ignore
+ }
+ }
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/DateExtensions.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/DateExtensions.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/DateExtensions.java
new file mode 100644
index 0000000..ea1a766
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/DateExtensions.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+
+public final class DateExtensions {
+
+ private DateExtensions(){}
+
+ public static String asTimestamp(final Date date) {
+ final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-hhmmssSSS");
+ return date == null ? "" : simpleDateFormat.format(date);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/HashCodeUtils.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/HashCodeUtils.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/HashCodeUtils.java
deleted file mode 100644
index a01fdcf..0000000
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/HashCodeUtils.java
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.commons.lang;
-
-import java.lang.reflect.Array;
-
-/**
- * Collected methods which allow easy implementation of <code>hashCode</code>,
- * based on Josh Bloch's Effective Java.
- *
- * <p>
- * Example use case:
- *
- * <pre>
- * public int hashCode() {
- * int result = HashCodeUtil.SEED;
- * // collect the contributions of various fields
- * result = HashCodeUtil.hash(result, fPrimitive);
- * result = HashCodeUtil.hash(result, fObject);
- * result = HashCodeUtil.hash(result, fArray);
- * return result;
- * }
- * </pre>
- *
- * @see http://www.javapractices.com/Topic28.cjp
- */
-public final class HashCodeUtils {
-
- private HashCodeUtils() {
- }
-
- /**
- * An initial value for a <code>hashCode</code>, to which is added
- * contributions from fields. Using a non-zero value decreases collisons of
- * <code>hashCode</code> values.
- */
- public static final int SEED = 23;
-
- /**
- * booleans.
- */
- public static int hash(final int aSeed, final boolean aBoolean) {
- return firstTerm(aSeed) + (aBoolean ? 1 : 0);
- }
-
- /**
- * chars.
- */
- public static int hash(final int aSeed, final char aChar) {
- return firstTerm(aSeed) + aChar;
- }
-
- /**
- * ints.
- *
- * <p>
- * Note that byte and short are handled by this method, through implicit
- * conversion.
- */
- public static int hash(final int aSeed, final int aInt) {
- return firstTerm(aSeed) + aInt;
- }
-
- /**
- * longs.
- */
- public static int hash(final int aSeed, final long aLong) {
- return firstTerm(aSeed) + (int) (aLong ^ (aLong >>> 32));
- }
-
- /**
- * floats.
- */
- public static int hash(final int aSeed, final float aFloat) {
- return hash(aSeed, Float.floatToIntBits(aFloat));
- }
-
- /**
- * doubles.
- */
- public static int hash(final int aSeed, final double aDouble) {
- return hash(aSeed, Double.doubleToLongBits(aDouble));
- }
-
- /**
- * <code>aObject</code> is a possibly-null object field, and possibly an
- * array.
- *
- * If <code>aObject</code> is an array, then each element may be a primitive
- * or a possibly-null object.
- */
- public static int hash(final int aSeed, final Object aObject) {
- int result = aSeed;
- if (aObject == null) {
- result = hash(result, 0);
- } else if (!isArray(aObject)) {
- result = hash(result, aObject.hashCode());
- } else {
- final int length = Array.getLength(aObject);
- for (int idx = 0; idx < length; ++idx) {
- final Object item = Array.get(aObject, idx);
- // recursive call!
- result = hash(result, item);
- }
- }
- return result;
- }
-
- private static final int ODD_PRIME_NUMBER = 37;
-
- private static int firstTerm(final int aSeed) {
- return ODD_PRIME_NUMBER * aSeed;
- }
-
- private static boolean isArray(final Object aObject) {
- return aObject.getClass().isArray();
- }
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/InputStreamExtensions.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/InputStreamExtensions.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/InputStreamExtensions.java
new file mode 100644
index 0000000..ec93d53
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/InputStreamExtensions.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+
+public final class InputStreamExtensions {
+
+ private static final int DEFAULT_BUFFER_SIZE = 1024;
+
+ private InputStreamExtensions() {
+ }
+
+ /**
+ * Copy bytes from an <code>InputStream</code> to an
+ * <code>OutputStream</code>.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * <code>BufferedInputStream</code>.
+ *
+ * @param extendee
+ * the <code>InputStream</code> to read from
+ * @param output
+ * the <code>OutputStream</code> to write to
+ * @return the number of bytes copied
+ * @throws IllegalArgumentException
+ * if the input or output is null
+ * @throws IOException
+ * if an I/O error occurs
+ * @since Commons IO 1.1
+ */
+ public static int copyTo(final InputStream extendee, final OutputStream output) throws IOException {
+ if (extendee == null) {
+ throw new IllegalArgumentException("InputStream cannot be null");
+ }
+ if (output == null) {
+ throw new IllegalArgumentException("OutputStream cannot be null");
+ }
+ final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+ int count = 0;
+ int n = 0;
+ while (-1 != (n = extendee.read(buffer))) {
+ output.write(buffer, 0, n);
+ count += n;
+ }
+ return count;
+ }
+
+ public static InputStream asUtf8ByteStream(final String string) throws UnsupportedEncodingException {
+ final byte[] data = string.getBytes("utf-8");
+ final InputStream in = new ByteArrayInputStream(data);
+ return in;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/IoUtils.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/IoUtils.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/IoUtils.java
deleted file mode 100644
index 29e56ef..0000000
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/IoUtils.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.commons.lang;
-
-import java.io.ByteArrayInputStream;
-import java.io.Closeable;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
-
-public final class IoUtils {
-
- private static final int DEFAULT_BUFFER_SIZE = 1024;
-
- private IoUtils() {
- }
-
- /**
- * Copy bytes from an <code>InputStream</code> to an
- * <code>OutputStream</code>.
- * <p>
- * This method buffers the input internally, so there is no need to use a
- * <code>BufferedInputStream</code>.
- *
- * @param input
- * the <code>InputStream</code> to read from
- * @param output
- * the <code>OutputStream</code> to write to
- * @return the number of bytes copied
- * @throws IllegalArgumentException
- * if the input or output is null
- * @throws IOException
- * if an I/O error occurs
- * @since Commons IO 1.1
- */
- public static int copy(final InputStream input, final OutputStream output) throws IOException {
- if (input == null) {
- throw new IllegalArgumentException("InputStream cannot be null");
- }
- if (output == null) {
- throw new IllegalArgumentException("OutputStream cannot be null");
- }
- final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
- int count = 0;
- int n = 0;
- while (-1 != (n = input.read(buffer))) {
- output.write(buffer, 0, n);
- count += n;
- }
- return count;
- }
-
- public static InputStream asUtf8ByteStream(final String string) throws UnsupportedEncodingException {
- final byte[] data = string.getBytes("utf-8");
- final InputStream in = new ByteArrayInputStream(data);
- return in;
- }
-
- public static void closeSafely(final Closeable closeable) {
- if (closeable != null) {
- try {
- closeable.close();
- } catch (final Exception ignore) {
- // ignore
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/JavaClassUtils.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/JavaClassUtils.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/JavaClassUtils.java
deleted file mode 100644
index 77e7f92..0000000
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/JavaClassUtils.java
+++ /dev/null
@@ -1,160 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.commons.lang;
-
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-public final class JavaClassUtils {
-
- private static final String JAVA_CLASS_PREFIX = "java.";
-
- private static Map<String, Class<?>> builtInClasses = new HashMap<String, Class<?>>();
-
- static {
- put(void.class);
- put(boolean.class);
- put(char.class);
- put(byte.class);
- put(short.class);
- put(int.class);
- put(long.class);
- put(float.class);
- put(double.class);
- }
-
- private static void put(final Class<?> cls) {
- builtInClasses.put(cls.getName(), cls);
- }
-
- private JavaClassUtils() {
- }
-
- public static Class<?> getBuiltIn(final String name) {
- return builtInClasses.get(name);
- }
-
- public static String getSuperclass(final Class<?> type) {
- final Class<?> superType = type.getSuperclass();
-
- if (superType == null) {
- return null;
- }
- return superType.getName();
- }
-
- public static boolean isAbstract(final Class<?> type) {
- return Modifier.isAbstract(type.getModifiers());
- }
-
- public static boolean isFinal(final Class<?> type) {
- return Modifier.isFinal(type.getModifiers());
- }
-
- public static boolean isPublic(final Class<?> type) {
- return Modifier.isPublic(type.getModifiers());
- }
-
- public static boolean isJavaClass(final Class<?> type) {
- return type.getName().startsWith(JAVA_CLASS_PREFIX) || type.getName().startsWith("sun.");
- }
-
- public static boolean isPublic(final Method method) {
- return Modifier.isPublic(method.getModifiers());
- }
-
- public static List<Class<?>> toClasses(final List<Object> objectList) {
- final List<Class<?>> classList = new ArrayList<Class<?>>();
- for (final Object service : objectList) {
- classList.add(service.getClass());
- }
- return classList;
- }
-
- /**
- * Returns the supplied Class so long as it implements (or is a subclass of)
- * the required class, and also has either a constructor accepting the
- * specified param type, or has a no-arg constructor.
- */
- public static Class<?> implementingClassOrNull(final Class<?> classCandidate, final Class<?> requiredClass, final Class<?> constructorParamType) {
- if (classCandidate == null) {
- return null;
- }
- if (!requiredClass.isAssignableFrom(classCandidate)) {
- return null;
- }
- try {
- classCandidate.getConstructor(new Class[] { constructorParamType });
- } catch (final NoSuchMethodException ex) {
- try {
- classCandidate.getConstructor(new Class[] {});
- } catch (final NoSuchMethodException e) {
- return null;
- }
- } catch (final SecurityException e) {
- return null;
- }
- final int modifiers = classCandidate.getModifiers();
- if (!Modifier.isPublic(modifiers)) {
- return null;
- }
- return classCandidate;
- }
-
- public static Class<?> implementingClassOrNull(final String classCandidateName, final Class<?> requiredClass, final Class<?> constructorParamType) {
- if (classCandidateName == null) {
- return null;
- }
- Class<?> classCandidate = null;
- try {
- classCandidate = Class.forName(classCandidateName);
- return implementingClassOrNull(classCandidate, requiredClass, constructorParamType);
- } catch (final ClassNotFoundException e) {
- return null;
- }
- }
-
- public static boolean directlyImplements(final Class<?> cls, final Class<?> interfaceType) {
- for (final Class<?> directlyImplementedInterface : cls.getInterfaces()) {
- if (directlyImplementedInterface == interfaceType) {
- return true;
- }
- }
- return false;
- }
-
- public static boolean directlyImplements(final Class<?> cls, final String interfaceTypeName) {
- try {
- final Class<?> interfaceType = Thread.currentThread().getContextClassLoader().loadClass(interfaceTypeName);
- return directlyImplements(cls, interfaceType);
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
-
- public static boolean isStatic(final Method method) {
- return Modifier.isStatic(method.getModifiers());
- }
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ListExtensions.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ListExtensions.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ListExtensions.java
new file mode 100644
index 0000000..792e852
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ListExtensions.java
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import com.google.common.collect.Collections2;
+import com.google.common.collect.Lists;
+
+public final class ListExtensions {
+ private static final String DEFAULT_DELIMITER = ",";
+
+ private ListExtensions() {
+ }
+
+ public static <T> List<T> combineWith(final List<T> extendee, final List<T> list2) {
+ final List<T> combinedList = Lists.newArrayList();
+ combinedList.addAll(extendee);
+ combinedList.addAll(list2);
+ return combinedList;
+ }
+
+ /**
+ * Returns list1 with everything in list2, ignoring duplicates.
+ */
+ public static <T> List<T> mergeWith(final List<T> extendee, final List<T> list2) {
+ for (final T obj : list2) {
+ if (!(extendee.contains(obj))) {
+ extendee.add(obj);
+ }
+ }
+ return extendee;
+ }
+
+
+ /**
+ * @see #listToString(List, String)
+ * @see #stringToList(String)
+ */
+ public static String listToString(final List<String> list) {
+ return listToString(list, DEFAULT_DELIMITER);
+ }
+
+ /**
+ * @see #listToString(List, String)
+ * @see #stringToList(String)
+ */
+ public static String listToString(final List<String> list, final String delimiter) {
+ if (list.size() == 0) {
+ return null;
+ }
+ final StringBuilder buf = new StringBuilder();
+ boolean first = true;
+ for (final String str : list) {
+ if (first) {
+ first = false;
+ } else {
+ buf.append(delimiter);
+ }
+ buf.append(str);
+ }
+ return buf.toString();
+ }
+
+ /**
+ * @see #stringToList(String, String)
+ * @see #listToString(List)
+ */
+ public static List<String> stringToList(final String commaSeparated) {
+ return appendDelimitedStringToList(commaSeparated, new ArrayList<String>());
+ }
+
+ /**
+ * @see #stringToList(String)
+ * @see #listToString(List, String)
+ */
+ public static List<String> stringToList(final String delimited, final String delimiter) {
+ return appendDelimitedStringToList(delimited, delimiter, new ArrayList<String>());
+ }
+
+ /**
+ * @see #appendDelimitedStringToList(String, String, List)
+ */
+ public static List<String> appendDelimitedStringToList(final String commaSeparated, final List<String> list) {
+ return appendDelimitedStringToList(commaSeparated, DEFAULT_DELIMITER, list);
+ }
+
+ public static List<String> appendDelimitedStringToList(final String delimited, final String delimiter, final List<String> list) {
+ if (delimited == null) {
+ return list;
+ }
+ final String[] optionValues = delimited.split(delimiter);
+ list.addAll(Arrays.asList(optionValues));
+ return list;
+ }
+
+ // //////////////////////////////////////
+
+ public static <T> List<T> mutableCopy(final List<T> input) {
+ return Lists.newArrayList(input != null? input: Collections.<T>emptyList());
+ }
+
+ public static <T> List<T> mutableCopy(T[] arr) {
+ return mutableCopy(arr != null? Arrays.asList(arr): Collections.<T>emptyList()) ;
+ }
+
+ public static <T> void insert(final List<T> list, final int insertionPoint, final T elementToInsert) {
+ extend(list, insertionPoint);
+ list.add(insertionPoint, elementToInsert);
+ }
+
+ public static <T> void adjust(final List<T> list, final int requiredLength) {
+ extend(list, requiredLength);
+ if(list.size() > requiredLength) {
+ list.subList(requiredLength, list.size()).clear();;
+ }
+ }
+
+ private static <T> void extend(final List<T> list, final int requiredLength) {
+ for(int i=list.size(); i<requiredLength; i++) {
+ list.add(null);
+ }
+ }
+
+ public static <T> Collection<T> filtered(final List<Object> extendee, final Class<T> type) {
+ return Collections2.transform(
+ Collections2.filter(extendee, ClassPredicates.isOfType(type)),
+ ClassFunctions.castTo(type));
+ }
+
+
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ListUtils.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ListUtils.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ListUtils.java
deleted file mode 100644
index 9e87d14..0000000
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/ListUtils.java
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.commons.lang;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-
-import com.google.common.collect.Lists;
-
-public final class ListUtils {
- private static final String DEFAULT_DELIMITER = ",";
-
- private ListUtils() {
- }
-
- public static <T> List<T> combine(final List<T> list, final List<T> list2) {
- final List<T> combinedList = new ArrayList<T>();
- combinedList.addAll(list);
- combinedList.addAll(list2);
- return combinedList;
- }
-
- /**
- * Returns list1 with everything in list2, ignoring duplicates.
- */
- public static <T> List<T> merge(final List<T> list1, final List<T> list2) {
- for (final T obj : list2) {
- if (!(list1.contains(obj))) {
- list1.add(obj);
- }
- }
- return list1;
- }
-
- public static List<String> merge(final String[] array1, final String[] array2) {
- final List<String> prefixes = new ArrayList<String>();
- addNoDuplicates(array1, prefixes);
- addNoDuplicates(array2, prefixes);
- return prefixes;
- }
-
- private static void addNoDuplicates(final String[] array, final List<String> list) {
- for (int i = 0; i < array.length; i++) {
- if (!list.contains(array[i])) {
- list.add(array[i]);
- }
- }
- }
-
- public static List<Object> asList(final Object[] objectArray) {
- final List<Object> list = new ArrayList<Object>();
- for (final Object element : objectArray) {
- if (Collection.class.isAssignableFrom(element.getClass())) {
- @SuppressWarnings("rawtypes")
- final Collection collection = (Collection) element;
- list.addAll(asList(collection.toArray()));
- } else {
- list.add(element);
- }
- }
- return list;
- }
-
- /**
- * @see #listToString(List, String)
- * @see #stringToList(String)
- */
- public static String listToString(final List<String> list) {
- return listToString(list, DEFAULT_DELIMITER);
- }
-
- /**
- * @see #listToString(List, String)
- * @see #stringToList(String)
- */
- public static String listToString(final List<String> list, final String delimiter) {
- if (list.size() == 0) {
- return null;
- }
- final StringBuilder buf = new StringBuilder();
- boolean first = true;
- for (final String str : list) {
- if (first) {
- first = false;
- } else {
- buf.append(delimiter);
- }
- buf.append(str);
- }
- return buf.toString();
- }
-
- /**
- * @see #stringToList(String, String)
- * @see #listToString(List)
- */
- public static List<String> stringToList(final String commaSeparated) {
- return appendDelimitedStringToList(commaSeparated, new ArrayList<String>());
- }
-
- /**
- * @see #stringToList(String)
- * @see #listToString(List, String)
- */
- public static List<String> stringToList(final String delimited, final String delimiter) {
- return appendDelimitedStringToList(delimited, delimiter, new ArrayList<String>());
- }
-
- /**
- * @see #appendDelimitedStringToList(String, String, List)
- */
- public static List<String> appendDelimitedStringToList(final String commaSeparated, final List<String> list) {
- return appendDelimitedStringToList(commaSeparated, DEFAULT_DELIMITER, list);
- }
-
- public static List<String> appendDelimitedStringToList(final String delimited, final String delimiter, final List<String> list) {
- if (delimited == null) {
- return list;
- }
- final String[] optionValues = delimited.split(delimiter);
- list.addAll(Arrays.asList(optionValues));
- return list;
- }
-
- // //////////////////////////////////////
-
- public static <T> List<T> mutableCopy(final List<T> input) {
- return Lists.newArrayList(input != null? input: Collections.<T>emptyList());
- }
-
- public static <T> List<T> mutableCopy(T[] arr) {
- return mutableCopy(arr != null? Arrays.asList(arr): Collections.<T>emptyList()) ;
- }
-
- public static <T> void insert(final List<T> list, final int insertionPoint, final T elementToInsert) {
- extend(list, insertionPoint);
- list.add(insertionPoint, elementToInsert);
- }
-
- public static <T> void adjust(final List<T> list, final int requiredLength) {
- extend(list, requiredLength);
- if(list.size() > requiredLength) {
- list.subList(requiredLength, list.size()).clear();;
- }
- }
-
- private static <T> void extend(final List<T> list, final int requiredLength) {
- for(int i=list.size(); i<requiredLength; i++) {
- list.add(null);
- }
- }
-
-
-
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/LocaleUtil.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/LocaleUtil.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/LocaleUtil.java
new file mode 100644
index 0000000..5e0b6d3
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/LocaleUtil.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.util.Locale;
+
+public class LocaleUtil {
+
+ public static Locale findLocale(final String localeStr) {
+ if (localeStr != null) {
+ final Locale[] availableLocales = Locale.getAvailableLocales();
+ for (final Locale locale : availableLocales) {
+ if (locale.toString().equals(localeStr)) {
+ return locale;
+ }
+ }
+ }
+ return null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/LocaleUtils.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/LocaleUtils.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/LocaleUtils.java
deleted file mode 100644
index 8cab421..0000000
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/LocaleUtils.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.commons.lang;
-
-import java.util.Locale;
-
-public class LocaleUtils {
-
- public static Locale findLocale(final String localeStr) {
- if (localeStr != null) {
- final Locale[] availableLocales = Locale.getAvailableLocales();
- for (final Locale locale : availableLocales) {
- if (locale.toString().equals(localeStr)) {
- return locale;
- }
- }
- }
- return null;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MapUtil.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MapUtil.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MapUtil.java
new file mode 100644
index 0000000..29f9a3d
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MapUtil.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public final class MapUtil {
+
+ private MapUtil() {
+ }
+
+ /**
+ * Converts a list of objects [a, 1, b, 2] into a map {a -> 1; b -> 2}
+ */
+ @SuppressWarnings("unchecked")
+ public static <K,V> Map<K,V> asMap(Object... keyValPair){
+ Map<K,V> map = new HashMap<K,V>();
+
+ if(keyValPair.length % 2 != 0){
+ throw new IllegalArgumentException("Keys and values must be pairs.");
+ }
+
+ for(int i = 0; i < keyValPair.length; i += 2){
+ map.put((K) keyValPair[i], (V) keyValPair[i+1]);
+ }
+
+ return Collections.unmodifiableMap(map);
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MapUtils.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MapUtils.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MapUtils.java
deleted file mode 100644
index 779243a..0000000
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MapUtils.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.commons.lang;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-public final class MapUtils {
-
- private MapUtils() {
- }
-
- /**
- * Converts a list of objects [a, 1, b, 2] into a map {a -> 1; b -> 2}
- */
- @SuppressWarnings("unchecked")
- public static <K,V> Map<K,V> asMap(Object... keyValPair){
- Map<K,V> map = new HashMap<K,V>();
-
- if(keyValPair.length % 2 != 0){
- throw new IllegalArgumentException("Keys and values must be pairs.");
- }
-
- for(int i = 0; i < keyValPair.length; i += 2){
- map.put((K) keyValPair[i], (V) keyValPair[i+1]);
- }
-
- return Collections.unmodifiableMap(map);
- }
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodExtensions.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodExtensions.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodExtensions.java
new file mode 100644
index 0000000..a2807e7
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodExtensions.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import org.apache.isis.core.metamodel.exceptions.MetaModelException;
+
+public class MethodExtensions {
+
+ private MethodExtensions() {
+ }
+
+ public static boolean isPublic(final Method method) {
+ return Modifier.isPublic(method.getModifiers());
+ }
+
+ public static boolean isStatic(final Method method) {
+ return Modifier.isStatic(method.getModifiers());
+ }
+
+ // //////////////////////////////////////
+
+ public static Object invoke(final Method method, final Object object) {
+ final Object[] parameters = MethodExtensions.getNullOrDefaultArgs(method);
+ return MethodExtensions.invoke(method, object, parameters);
+ }
+
+ public static Object invoke(final Method method, final Object object, final Object[] parameters) {
+ try {
+ return method.invoke(object, parameters);
+ } catch (final IllegalArgumentException e) {
+ throw e;
+ } catch (final InvocationTargetException e) {
+ ThrowableExtensions.throwWithinIsisException(e, "Exception executing " + method);
+ return null;
+ } catch (final IllegalAccessException e) {
+ throw new MetaModelException("illegal access of " + method, e);
+ }
+ }
+
+ public static Object invokeStatic(final Method method, final Object[] parameters) {
+ return invoke(method, null, parameters);
+ }
+
+ public static Object invokeStatic(final Method method) {
+ return invoke(method, null, MethodExtensions.getNullOrDefaultArgs(method));
+ }
+
+ // //////////////////////////////////////
+
+
+ public static Object[] getNullOrDefaultArgs(final Method method) {
+ final Class<?>[] paramTypes = method.getParameterTypes();
+ final Object[] parameters = new Object[paramTypes.length];
+ for (int i = 0; i < parameters.length; i++) {
+ parameters[i] = ClassExtensions.getNullOrDefault(paramTypes[i]);
+ }
+ return parameters;
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtil.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtil.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtil.java
new file mode 100644
index 0000000..f526c64
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtil.java
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.isis.core.commons.lang;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.isis.core.metamodel.methodutils.MethodScope;
+
+public class MethodUtil {
+
+ private MethodUtil(){}
+
+ public static void invoke(final List<Method> methods, final Object object) {
+ for (final Method method : methods) {
+ MethodExtensions.invoke(method, object);
+ }
+ }
+
+ /**
+ * Searches the supplied array of methods for specific method and returns
+ * it, also removing it from supplied array if found (by setting to
+ * <tt>null</tt>).
+ *
+ * <p>
+ * Any methods that do not meet the search criteria are left in the array of
+ * methods.
+ *
+ * <p>
+ * The search algorithm is as specified in
+ * {@link MethodUtil#findMethodIndex(List, MethodScope, String, Class, Class[])}.
+ */
+ public static Method removeMethod(final List<Method> methods, final MethodScope methodScope, final String name, final Class<?> returnType, final Class<?>[] paramTypes) {
+ final int idx = MethodUtil.findMethodIndex(methods, methodScope, name, returnType, paramTypes);
+ if (idx != -1) {
+ final Method method = methods.get(idx);
+ methods.set(idx, null);
+ return method;
+ }
+ return null;
+ }
+
+ /**
+ * Searches the supplied array of methods for specific method and returns
+ * its index, otherwise returns <tt>-1</tt>.
+ *
+ * <p>
+ * The search algorithm is:
+ * <ul>
+ * <li>has the specified prefix</li>
+ * <li>has the specified return type, or <tt>void</tt> if canBeVoid is
+ * <tt>true</tt> (but see below)</li>
+ * <li>has the specified number of parameters</li>
+ * </ul>
+ * If the returnType is specified as null then the return type is ignored.
+ */
+ public static int findMethodIndex(final List<Method> methods, final MethodScope methodScope, final String name, final Class<?> returnType, final Class<?>[] paramTypes) {
+ int idx = -1;
+ method: for (int i = 0; i < methods.size(); i++) {
+ if (methods.get(i) == null) {
+ continue;
+ }
+
+ final Method method = methods.get(i);
+ final int modifiers = method.getModifiers();
+
+ // check for public modifier
+ if (!Modifier.isPublic(modifiers)) {
+ continue;
+ }
+
+ // check for static modifier
+ if (!inScope(method, methodScope)) {
+ continue;
+ }
+
+ // check for name
+ if (!method.getName().equals(name)) {
+ continue;
+ }
+
+ // check for return type
+ if (returnType != null && returnType != method.getReturnType()) {
+ continue;
+ }
+
+ // check params (if required)
+ if (paramTypes != null) {
+ final Class<?>[] parameterTypes = method.getParameterTypes();
+ if (paramTypes.length != parameterTypes.length) {
+ continue;
+ }
+
+ for (int c = 0; c < paramTypes.length; c++) {
+ if ((paramTypes[c] != null) && (paramTypes[c] != parameterTypes[c])) {
+ continue method;
+ }
+ }
+ }
+ idx = i;
+ break;
+ }
+ return idx;
+ }
+
+ public static boolean inScope(final Method extendee, final MethodScope methodScope) {
+ final boolean isStatic = MethodExtensions.isStatic(extendee);
+ return isStatic && methodScope == MethodScope.CLASS || !isStatic && methodScope == MethodScope.OBJECT;
+ }
+
+ /**
+ * Searches the supplied array of methods for all specific methods and
+ * returns them, also removing them from supplied array if found.
+ *
+ * <p>
+ * Any methods that do not meet the search criteria are left in the array of
+ * methods.
+ *
+ * <p>
+ * The search algorithm is:
+ * <ul>
+ * <li>has the specified prefix</li>
+ * <li>has the specified return type, or <tt>void</tt> if canBeVoid is
+ * <tt>true</tt> (but see below)</li>
+ * <li>has the specified number of parameters</li>
+ * </ul>
+ * If the returnType is specified as null then the return type is ignored.
+ *
+ * @param forClass
+ * @param name
+ * @param returnType
+ * @param paramTypes
+ * the set of parameters the method should have, if null then is
+ * ignored
+ * @return Method
+ */
+ public static List<Method> removeMethods(final List<Method> methods, final MethodScope forClass, final String prefix, final Class<?> returnType, final boolean canBeVoid, final int paramCount) {
+
+ final List<Method> validMethods = new ArrayList<Method>();
+
+ for (int i = 0; i < methods.size(); i++) {
+ if (methods.get(i) == null) {
+ continue;
+ }
+
+ final Method method = methods.get(i);
+
+ if (!inScope(method, forClass)) {
+ continue;
+ }
+
+ final boolean goodPrefix = method.getName().startsWith(prefix);
+
+ final boolean goodCount = method.getParameterTypes().length == paramCount;
+ final Class<?> type = method.getReturnType();
+ final boolean goodReturn = ClassExtensions.isCompatibleAsReturnType(returnType, canBeVoid, type);
+
+ if (goodPrefix && goodCount && goodReturn) {
+ validMethods.add(method);
+ methods.set(i, null);
+ }
+ }
+ return validMethods;
+ }
+
+ /**
+ * From the supplied method array, finds but <i>does not remove</i> methods
+ * that have the required prefix, and adds to the supplied candidates
+ * vector.
+ */
+ public static void findPrefixedInstanceMethods(final Method[] methods, final String prefix, final List<Method> candidates) {
+ for (final Method method : methods) {
+ if (method == null) {
+ continue;
+ }
+
+ if (MethodExtensions.isStatic(method)) {
+ continue;
+ }
+
+ final boolean goodPrefix = method.getName().startsWith(prefix);
+ final boolean goodCount = method.getParameterTypes().length == 0;
+
+ if (goodPrefix && goodCount) {
+ candidates.add(method);
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtils.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtils.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtils.java
deleted file mode 100644
index 964ee56..0000000
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/MethodUtils.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.commons.lang;
-
-import java.lang.reflect.Method;
-
-public class MethodUtils {
-
- private MethodUtils() {
- }
-
- public static Method getMethod(final Object object, final String methodName, final Class<?>... parameterClass) throws NoSuchMethodException {
- return getMethod(object.getClass(), methodName, parameterClass);
- }
-
- public static Method getMethod(final Object object, final String methodName) throws NoSuchMethodException {
- return getMethod(object.getClass(), methodName, new Class[0]);
- }
-
- public static Method getMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterClass) throws NoSuchMethodException {
- return clazz.getMethod(methodName, parameterClass);
- }
-
- public static Method findMethodElseNull(final Class<?> clazz, final String methodName, final Class<?>... parameterClass) {
- try {
- return clazz.getMethod(methodName, parameterClass);
- } catch (final NoSuchMethodException e) {
- return null;
- }
- }
-
- public static Method findMethodElseNull(final Class<?> clazz, final String[] candidateMethodNames, final Class<?>... parameterClass) {
- for (final String candidateMethodName : candidateMethodNames) {
- final Method method = findMethodElseNull(clazz, candidateMethodName, parameterClass);
- if (method != null) {
- return method;
- }
- }
- return null;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/91df9978/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/NameUtils.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/NameUtils.java b/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/NameUtils.java
deleted file mode 100644
index 8623afa..0000000
--- a/core/metamodel/src/main/java/org/apache/isis/core/commons/lang/NameUtils.java
+++ /dev/null
@@ -1,171 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.isis.core.commons.lang;
-
-public final class NameUtils {
-
- private static final char SPACE = ' ';
-
- private NameUtils() {
- }
-
- /**
- * Returns the name of a Java entity without any prefix. A prefix is defined
- * as the first set of lowercase letters and the name is characters from,
- * and including, the first upper case letter. If no upper case letter is
- * found then an empty string is returned.
- *
- * <p>
- * Calling this method with the following Java names will produce these
- * results:
- *
- * <pre>
- * getCarRegistration -> CarRegistration
- * CityMayor -> CityMayor
- * isReady -> Ready
- * </pre>
- *
- */
- public static String javaBaseName(final String javaName) {
- int pos = 0;
-
- // find first upper case character
- final int len = javaName.length();
-
- while ((pos < len) && (javaName.charAt(pos) != '_') && Character.isLowerCase(javaName.charAt(pos))) {
- pos++;
- }
-
- if (pos >= len) {
- return "";
- }
-
- if (javaName.charAt(pos) == '_') {
- pos++;
- }
-
- if (pos >= len) {
- return "";
- }
-
- final String baseName = javaName.substring(pos);
- final char firstChar = baseName.charAt(0);
-
- if (Character.isLowerCase(firstChar)) {
- return Character.toUpperCase(firstChar) + baseName.substring(1);
- } else {
- return baseName;
- }
- }
-
- public static String javaBaseNameStripAccessorPrefixIfRequired(final String javaName) {
- if (javaName.startsWith("is") || javaName.startsWith("get")) {
- return javaBaseName(javaName);
- } else {
- return capitalizeName(javaName);
- }
- }
-
- public static String capitalizeName(final String name) {
- return Character.toUpperCase(name.charAt(0)) + name.substring(1);
- }
-
- public static boolean startsWith(final String fullMethodName, final String prefix) {
- final int length = prefix.length();
- if (length >= fullMethodName.length()) {
- return false;
- } else {
- final char startingCharacter = fullMethodName.charAt(length);
- return fullMethodName.startsWith(prefix) && Character.isUpperCase(startingCharacter);
- }
- }
-
- /**
- * Return a lower case, non-spaced version of the specified name.
- */
- public static String simpleName(final String name) {
- final int len = name.length();
- final StringBuffer sb = new StringBuffer(len);
- for (int pos = 0; pos < len; pos++) {
- final char ch = name.charAt(pos);
- if (ch == ' ') {
- continue;
- }
- sb.append(Character.toLowerCase(ch));
- }
- return sb.toString();
- }
-
- /**
- * Returns a word spaced version of the specified name, so there are spaces
- * between the words, where each word starts with a capital letter. E.g.,
- * "NextAvailableDate" is returned as "Next Available Date".
- */
- public static String naturalName(final String name) {
-
- final int length = name.length();
-
- if (length <= 1) {
- return name.toUpperCase();// ensure first character is upper case
- }
-
- final StringBuffer naturalName = new StringBuffer(length);
-
- char previousCharacter;
- char character = Character.toUpperCase(name.charAt(0));// ensure first
- // character is
- // upper case
- naturalName.append(character);
- char nextCharacter = name.charAt(1);
-
- for (int pos = 2; pos < length; pos++) {
- previousCharacter = character;
- character = nextCharacter;
- nextCharacter = name.charAt(pos);
-
- if (previousCharacter != NameUtils.SPACE) {
- if (Character.isUpperCase(character) && !Character.isUpperCase(previousCharacter)) {
- naturalName.append(NameUtils.SPACE);
- }
- if (Character.isUpperCase(character) && Character.isLowerCase(nextCharacter) && Character.isUpperCase(previousCharacter)) {
- naturalName.append(NameUtils.SPACE);
- }
- if (Character.isDigit(character) && !Character.isDigit(previousCharacter)) {
- naturalName.append(NameUtils.SPACE);
- }
- }
- naturalName.append(character);
- }
- naturalName.append(nextCharacter);
- return naturalName.toString();
- }
-
- public static String pluralName(final String name) {
- String pluralName;
- if (name.endsWith("y")) {
- pluralName = name.substring(0, name.length() - 1) + "ies";
- } else if (name.endsWith("s") || name.endsWith("x")) {
- pluralName = name + "es";
- } else {
- pluralName = name + 's';
- }
- return pluralName;
- }
-}