You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pp...@apache.org on 2009/12/10 01:00:09 UTC
svn commit: r889024 [3/4] - in /openjpa/trunk/contrib: ./ devtools/
devtools/openjpa-eclipse-testproject/
devtools/openjpa-eclipse-testproject/bin/
devtools/openjpa-eclipse-testproject/bin/test/
devtools/openjpa-eclipse-testproject/bin/test/test/ devto...
Added: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/AntPathStringMatcher.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/AntPathStringMatcher.java?rev=889024&view=auto
==============================================================================
--- openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/AntPathStringMatcher.java (added)
+++ openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/AntPathStringMatcher.java Thu Dec 10 00:00:07 2009
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2002-2009 the original author or authors.
+ *
+ * Licensed 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.openjpa.eclipse.util.pathmatch;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Package-protected helper class for {@link AntPathMatcher}. Tests whether or not a string matches against a pattern
+ * using a regular expression.
+ *
+ * <p>The pattern may contain special characters: '*' means zero or more characters; '?' means one and only one
+ * character; '{' and '}' indicate a URI template pattern.
+ *
+ * @author Arjen Poutsma
+ * @since 3.0
+ */
+class AntPathStringMatcher {
+
+ private static final Pattern GLOB_PATTERN = Pattern.compile("\\?|\\*|\\{([^/]+?)\\}");
+
+ private static final String DEFAULT_VARIABLE_PATTERN = "(.*)";
+
+ private final Pattern pattern;
+
+ private String str;
+
+ private final List<String> variableNames = new LinkedList<String>();
+
+ private final Map<String, String> uriTemplateVariables;
+
+ /** Construct a new instance of the <code>AntPatchStringMatcher</code>. */
+ AntPathStringMatcher(String pattern, String str, Map<String, String> uriTemplateVariables) {
+ this.str = str;
+ this.uriTemplateVariables = uriTemplateVariables;
+ this.pattern = createPattern(pattern);
+ }
+
+ private Pattern createPattern(String pattern) {
+ StringBuilder patternBuilder = new StringBuilder();
+ Matcher m = GLOB_PATTERN.matcher(pattern);
+ int end = 0;
+ while (m.find()) {
+ patternBuilder.append(quote(pattern, end, m.start()));
+ String match = m.group();
+ if ("?".equals(match)) {
+ patternBuilder.append('.');
+ }
+ else if ("*".equals(match)) {
+ patternBuilder.append(".*");
+ }
+ else if (match.startsWith("{") && match.endsWith("}")) {
+ int colonIdx = match.indexOf(':');
+ if (colonIdx == -1) {
+ patternBuilder.append(DEFAULT_VARIABLE_PATTERN);
+ variableNames.add(m.group(1));
+ }
+ else {
+ String variablePattern = match.substring(colonIdx + 1, match.length() - 1);
+ patternBuilder.append('(');
+ patternBuilder.append(variablePattern);
+ patternBuilder.append(')');
+ String variableName = match.substring(1, colonIdx);
+ variableNames.add(variableName);
+ }
+ }
+ end = m.end();
+ }
+ patternBuilder.append(quote(pattern, end, pattern.length()));
+ return Pattern.compile(patternBuilder.toString());
+ }
+
+ private String quote(String s, int start, int end) {
+ if (start == end) {
+ return "";
+ }
+ return Pattern.quote(s.substring(start, end));
+ }
+
+ /**
+ * Main entry point.
+ *
+ * @return <code>true</code> if the string matches against the pattern, or <code>false</code> otherwise.
+ */
+ public boolean matchStrings() {
+ Matcher matcher = pattern.matcher(str);
+ if (matcher.matches()) {
+ if (uriTemplateVariables != null) {
+ for (int i = 1; i <= matcher.groupCount(); i++) {
+ String name = this.variableNames.get(i - 1);
+ String value = matcher.group(i);
+ uriTemplateVariables.put(name, value);
+ }
+ }
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+}
Propchange: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/AntPathStringMatcher.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/Assert.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/Assert.java?rev=889024&view=auto
==============================================================================
--- openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/Assert.java (added)
+++ openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/Assert.java Thu Dec 10 00:00:07 2009
@@ -0,0 +1,402 @@
+/*
+ * Copyright 2002-2007 the original author or authors.
+ *
+ * Licensed 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.openjpa.eclipse.util.pathmatch;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Assertion utility class that assists in validating arguments.
+ * Useful for identifying programmer errors early and clearly at runtime.
+ *
+ * <p>For example, if the contract of a public method states it does not
+ * allow <code>null</code> arguments, Assert can be used to validate that
+ * contract. Doing this clearly indicates a contract violation when it
+ * occurs and protects the class's invariants.
+ *
+ * <p>Typically used to validate method arguments rather than configuration
+ * properties, to check for cases that are usually programmer errors rather than
+ * configuration errors. In contrast to config initialization code, there is
+ * usally no point in falling back to defaults in such methods.
+ *
+ * <p>This class is similar to JUnit's assertion library. If an argument value is
+ * deemed invalid, an {@link IllegalArgumentException} is thrown (typically).
+ * For example:
+ *
+ * <pre class="code">
+ * Assert.notNull(clazz, "The class must not be null");
+ * Assert.isTrue(i > 0, "The value must be greater than zero");</pre>
+ *
+ * Mainly for internal use within the framework; consider Jakarta's Commons Lang
+ * >= 2.0 for a more comprehensive suite of assertion utilities.
+ *
+ * @author Keith Donald
+ * @author Juergen Hoeller
+ * @author Colin Sampaleanu
+ * @author Rob Harrop
+ * @since 1.1.2
+ */
+@SuppressWarnings("unchecked")
+public abstract class Assert {
+
+ /**
+ * Assert a boolean expression, throwing <code>IllegalArgumentException</code>
+ * if the test result is <code>false</code>.
+ * <pre class="code">Assert.isTrue(i > 0, "The value must be greater than zero");</pre>
+ * @param expression a boolean expression
+ * @param message the exception message to use if the assertion fails
+ * @throws IllegalArgumentException if expression is <code>false</code>
+ */
+ public static void isTrue(boolean expression, String message) {
+ if (!expression) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert a boolean expression, throwing <code>IllegalArgumentException</code>
+ * if the test result is <code>false</code>.
+ * <pre class="code">Assert.isTrue(i > 0);</pre>
+ * @param expression a boolean expression
+ * @throws IllegalArgumentException if expression is <code>false</code>
+ */
+ public static void isTrue(boolean expression) {
+ isTrue(expression, "[Assertion failed] - this expression must be true");
+ }
+
+ /**
+ * Assert that an object is <code>null</code> .
+ * <pre class="code">Assert.isNull(value, "The value must be null");</pre>
+ * @param object the object to check
+ * @param message the exception message to use if the assertion fails
+ * @throws IllegalArgumentException if the object is not <code>null</code>
+ */
+ public static void isNull(Object object, String message) {
+ if (object != null) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert that an object is <code>null</code> .
+ * <pre class="code">Assert.isNull(value);</pre>
+ * @param object the object to check
+ * @throws IllegalArgumentException if the object is not <code>null</code>
+ */
+ public static void isNull(Object object) {
+ isNull(object, "[Assertion failed] - the object argument must be null");
+ }
+
+ /**
+ * Assert that an object is not <code>null</code> .
+ * <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
+ * @param object the object to check
+ * @param message the exception message to use if the assertion fails
+ * @throws IllegalArgumentException if the object is <code>null</code>
+ */
+ public static void notNull(Object object, String message) {
+ if (object == null) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert that an object is not <code>null</code> .
+ * <pre class="code">Assert.notNull(clazz);</pre>
+ * @param object the object to check
+ * @throws IllegalArgumentException if the object is <code>null</code>
+ */
+ public static void notNull(Object object) {
+ notNull(object, "[Assertion failed] - this argument is required; it must not be null");
+ }
+
+ /**
+ * Assert that the given String is not empty; that is,
+ * it must not be <code>null</code> and not the empty String.
+ * <pre class="code">Assert.hasLength(name, "Name must not be empty");</pre>
+ * @param text the String to check
+ * @param message the exception message to use if the assertion fails
+ * @see StringUtils#hasLength
+ */
+ public static void hasLength(String text, String message) {
+ if (!StringUtils.hasLength(text)) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert that the given String is not empty; that is,
+ * it must not be <code>null</code> and not the empty String.
+ * <pre class="code">Assert.hasLength(name);</pre>
+ * @param text the String to check
+ * @see StringUtils#hasLength
+ */
+ public static void hasLength(String text) {
+ hasLength(text,
+ "[Assertion failed] - this String argument must have length; it must not be null or empty");
+ }
+
+ /**
+ * Assert that the given String has valid text content; that is, it must not
+ * be <code>null</code> and must contain at least one non-whitespace character.
+ * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
+ * @param text the String to check
+ * @param message the exception message to use if the assertion fails
+ * @see StringUtils#hasText
+ */
+ public static void hasText(String text, String message) {
+ if (!StringUtils.hasText(text)) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert that the given String has valid text content; that is, it must not
+ * be <code>null</code> and must contain at least one non-whitespace character.
+ * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
+ * @param text the String to check
+ * @see StringUtils#hasText
+ */
+ public static void hasText(String text) {
+ hasText(text,
+ "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
+ }
+
+ /**
+ * Assert that the given text does not contain the given substring.
+ * <pre class="code">Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");</pre>
+ * @param textToSearch the text to search
+ * @param substring the substring to find within the text
+ * @param message the exception message to use if the assertion fails
+ */
+ public static void doesNotContain(String textToSearch, String substring, String message) {
+ if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
+ textToSearch.indexOf(substring) != -1) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert that the given text does not contain the given substring.
+ * <pre class="code">Assert.doesNotContain(name, "rod");</pre>
+ * @param textToSearch the text to search
+ * @param substring the substring to find within the text
+ */
+ public static void doesNotContain(String textToSearch, String substring) {
+ doesNotContain(textToSearch, substring,
+ "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
+ }
+
+
+ /**
+ * Assert that an array has elements; that is, it must not be
+ * <code>null</code> and must have at least one element.
+ * <pre class="code">Assert.notEmpty(array, "The array must have elements");</pre>
+ * @param array the array to check
+ * @param message the exception message to use if the assertion fails
+ * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
+ */
+ public static void notEmpty(Object[] array, String message) {
+ if (ObjectUtils.isEmpty(array)) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert that an array has elements; that is, it must not be
+ * <code>null</code> and must have at least one element.
+ * <pre class="code">Assert.notEmpty(array);</pre>
+ * @param array the array to check
+ * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
+ */
+ public static void notEmpty(Object[] array) {
+ notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
+ }
+
+ /**
+ * Assert that an array has no null elements.
+ * Note: Does not complain if the array is empty!
+ * <pre class="code">Assert.noNullElements(array, "The array must have non-null elements");</pre>
+ * @param array the array to check
+ * @param message the exception message to use if the assertion fails
+ * @throws IllegalArgumentException if the object array contains a <code>null</code> element
+ */
+ public static void noNullElements(Object[] array, String message) {
+ if (array != null) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == null) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+ }
+ }
+
+ /**
+ * Assert that an array has no null elements.
+ * Note: Does not complain if the array is empty!
+ * <pre class="code">Assert.noNullElements(array);</pre>
+ * @param array the array to check
+ * @throws IllegalArgumentException if the object array contains a <code>null</code> element
+ */
+ public static void noNullElements(Object[] array) {
+ noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
+ }
+
+ /**
+ * Assert that a collection has elements; that is, it must not be
+ * <code>null</code> and must have at least one element.
+ * <pre class="code">Assert.notEmpty(collection, "Collection must have elements");</pre>
+ * @param collection the collection to check
+ * @param message the exception message to use if the assertion fails
+ * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
+ */
+ public static void notEmpty(Collection collection, String message) {
+ if (CollectionUtils.isEmpty(collection)) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert that a collection has elements; that is, it must not be
+ * <code>null</code> and must have at least one element.
+ * <pre class="code">Assert.notEmpty(collection, "Collection must have elements");</pre>
+ * @param collection the collection to check
+ * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
+ */
+ public static void notEmpty(Collection collection) {
+ notEmpty(collection,
+ "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
+ }
+
+ /**
+ * Assert that a Map has entries; that is, it must not be <code>null</code>
+ * and must have at least one entry.
+ * <pre class="code">Assert.notEmpty(map, "Map must have entries");</pre>
+ * @param map the map to check
+ * @param message the exception message to use if the assertion fails
+ * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
+ */
+ public static void notEmpty(Map map, String message) {
+ if (CollectionUtils.isEmpty(map)) {
+ throw new IllegalArgumentException(message);
+ }
+ }
+
+ /**
+ * Assert that a Map has entries; that is, it must not be <code>null</code>
+ * and must have at least one entry.
+ * <pre class="code">Assert.notEmpty(map);</pre>
+ * @param map the map to check
+ * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
+ */
+ public static void notEmpty(Map map) {
+ notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
+ }
+
+
+ /**
+ * Assert that the provided object is an instance of the provided class.
+ * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
+ * @param clazz the required class
+ * @param obj the object to check
+ * @throws IllegalArgumentException if the object is not an instance of clazz
+ * @see Class#isInstance
+ */
+ public static void isInstanceOf(Class clazz, Object obj) {
+ isInstanceOf(clazz, obj, "");
+ }
+
+ /**
+ * Assert that the provided object is an instance of the provided class.
+ * <pre class="code">Assert.instanceOf(Foo.class, foo);</pre>
+ * @param type the type to check against
+ * @param obj the object to check
+ * @param message a message which will be prepended to the message produced by
+ * the function itself, and which may be used to provide context. It should
+ * normally end in a ": " or ". " so that the function generate message looks
+ * ok when prepended to it.
+ * @throws IllegalArgumentException if the object is not an instance of clazz
+ * @see Class#isInstance
+ */
+ public static void isInstanceOf(Class type, Object obj, String message) {
+ notNull(type, "Type to check against must not be null");
+ if (!type.isInstance(obj)) {
+ throw new IllegalArgumentException(message +
+ "Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
+ "] must be an instance of " + type);
+ }
+ }
+
+ /**
+ * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>.
+ * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
+ * @param superType the super type to check
+ * @param subType the sub type to check
+ * @throws IllegalArgumentException if the classes are not assignable
+ */
+ public static void isAssignable(Class superType, Class subType) {
+ isAssignable(superType, subType, "");
+ }
+
+ /**
+ * Assert that <code>superType.isAssignableFrom(subType)</code> is <code>true</code>.
+ * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
+ * @param superType the super type to check against
+ * @param subType the sub type to check
+ * @param message a message which will be prepended to the message produced by
+ * the function itself, and which may be used to provide context. It should
+ * normally end in a ": " or ". " so that the function generate message looks
+ * ok when prepended to it.
+ * @throws IllegalArgumentException if the classes are not assignable
+ */
+ public static void isAssignable(Class superType, Class subType, String message) {
+ notNull(superType, "Type to check against must not be null");
+ if (subType == null || !superType.isAssignableFrom(subType)) {
+ throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);
+ }
+ }
+
+
+ /**
+ * Assert a boolean expression, throwing <code>IllegalStateException</code>
+ * if the test result is <code>false</code>. Call isTrue if you wish to
+ * throw IllegalArgumentException on an assertion failure.
+ * <pre class="code">Assert.state(id == null, "The id property must not already be initialized");</pre>
+ * @param expression a boolean expression
+ * @param message the exception message to use if the assertion fails
+ * @throws IllegalStateException if expression is <code>false</code>
+ */
+ public static void state(boolean expression, String message) {
+ if (!expression) {
+ throw new IllegalStateException(message);
+ }
+ }
+
+ /**
+ * Assert a boolean expression, throwing {@link IllegalStateException}
+ * if the test result is <code>false</code>.
+ * <p>Call {@link #isTrue(boolean)} if you wish to
+ * throw {@link IllegalArgumentException} on an assertion failure.
+ * <pre class="code">Assert.state(id == null);</pre>
+ * @param expression a boolean expression
+ * @throws IllegalStateException if the supplied expression is <code>false</code>
+ */
+ public static void state(boolean expression) {
+ state(expression, "[Assertion failed] - this state invariant must be true");
+ }
+
+}
Propchange: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/Assert.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/CollectionUtils.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/CollectionUtils.java?rev=889024&view=auto
==============================================================================
--- openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/CollectionUtils.java (added)
+++ openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/CollectionUtils.java Thu Dec 10 00:00:07 2009
@@ -0,0 +1,310 @@
+/*
+ * Copyright 2002-2009 the original author or authors.
+ *
+ * Licensed 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.openjpa.eclipse.util.pathmatch;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Miscellaneous collection utility methods.
+ * Mainly for internal use within the framework.
+ *
+ * @author Juergen Hoeller
+ * @author Rob Harrop
+ * @since 1.1.3
+ */
+@SuppressWarnings("unchecked")
+public abstract class CollectionUtils {
+
+ /**
+ * Return <code>true</code> if the supplied Collection is <code>null</code>
+ * or empty. Otherwise, return <code>false</code>.
+ * @param collection the Collection to check
+ * @return whether the given Collection is empty
+ */
+ public static boolean isEmpty(Collection collection) {
+ return (collection == null || collection.isEmpty());
+ }
+
+ /**
+ * Return <code>true</code> if the supplied Map is <code>null</code>
+ * or empty. Otherwise, return <code>false</code>.
+ * @param map the Map to check
+ * @return whether the given Map is empty
+ */
+ public static boolean isEmpty(Map map) {
+ return (map == null || map.isEmpty());
+ }
+
+ /**
+ * Convert the supplied array into a List. A primitive array gets
+ * converted into a List of the appropriate wrapper type.
+ * <p>A <code>null</code> source value will be converted to an
+ * empty List.
+ * @param source the (potentially primitive) array
+ * @return the converted List result
+ * @see ObjectUtils#toObjectArray(Object)
+ */
+ public static List arrayToList(Object source) {
+ return Arrays.asList(ObjectUtils.toObjectArray(source));
+ }
+
+ /**
+ * Merge the given array into the given Collection.
+ * @param array the array to merge (may be <code>null</code>)
+ * @param collection the target Collection to merge the array into
+ */
+ public static void mergeArrayIntoCollection(Object array, Collection collection) {
+ if (collection == null) {
+ throw new IllegalArgumentException("Collection must not be null");
+ }
+ Object[] arr = ObjectUtils.toObjectArray(array);
+ for (Object elem : arr) {
+ collection.add(elem);
+ }
+ }
+
+ /**
+ * Merge the given Properties instance into the given Map,
+ * copying all properties (key-value pairs) over.
+ * <p>Uses <code>Properties.propertyNames()</code> to even catch
+ * default properties linked into the original Properties instance.
+ * @param props the Properties instance to merge (may be <code>null</code>)
+ * @param map the target Map to merge the properties into
+ */
+ public static void mergePropertiesIntoMap(Properties props, Map map) {
+ if (map == null) {
+ throw new IllegalArgumentException("Map must not be null");
+ }
+ if (props != null) {
+ for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {
+ String key = (String) en.nextElement();
+ Object value = props.getProperty(key);
+ if (value == null) {
+ // Potentially a non-String value...
+ value = props.get(key);
+ }
+ map.put(key, value);
+ }
+ }
+ }
+
+
+ /**
+ * Check whether the given Iterator contains the given element.
+ * @param iterator the Iterator to check
+ * @param element the element to look for
+ * @return <code>true</code> if found, <code>false</code> else
+ */
+ public static boolean contains(Iterator iterator, Object element) {
+ if (iterator != null) {
+ while (iterator.hasNext()) {
+ Object candidate = iterator.next();
+ if (ObjectUtils.nullSafeEquals(candidate, element)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Check whether the given Enumeration contains the given element.
+ * @param enumeration the Enumeration to check
+ * @param element the element to look for
+ * @return <code>true</code> if found, <code>false</code> else
+ */
+ public static boolean contains(Enumeration enumeration, Object element) {
+ if (enumeration != null) {
+ while (enumeration.hasMoreElements()) {
+ Object candidate = enumeration.nextElement();
+ if (ObjectUtils.nullSafeEquals(candidate, element)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Check whether the given Collection contains the given element instance.
+ * <p>Enforces the given instance to be present, rather than returning
+ * <code>true</code> for an equal element as well.
+ * @param collection the Collection to check
+ * @param element the element to look for
+ * @return <code>true</code> if found, <code>false</code> else
+ */
+ public static boolean containsInstance(Collection collection, Object element) {
+ if (collection != null) {
+ for (Object candidate : collection) {
+ if (candidate == element) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Return <code>true</code> if any element in '<code>candidates</code>' is
+ * contained in '<code>source</code>'; otherwise returns <code>false</code>.
+ * @param source the source Collection
+ * @param candidates the candidates to search for
+ * @return whether any of the candidates has been found
+ */
+ public static boolean containsAny(Collection source, Collection candidates) {
+ if (isEmpty(source) || isEmpty(candidates)) {
+ return false;
+ }
+ for (Object candidate : candidates) {
+ if (source.contains(candidate)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Return the first element in '<code>candidates</code>' that is contained in
+ * '<code>source</code>'. If no element in '<code>candidates</code>' is present in
+ * '<code>source</code>' returns <code>null</code>. Iteration order is
+ * {@link Collection} implementation specific.
+ * @param source the source Collection
+ * @param candidates the candidates to search for
+ * @return the first present object, or <code>null</code> if not found
+ */
+ public static Object findFirstMatch(Collection source, Collection candidates) {
+ if (isEmpty(source) || isEmpty(candidates)) {
+ return null;
+ }
+ for (Object candidate : candidates) {
+ if (source.contains(candidate)) {
+ return candidate;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Find a single value of the given type in the given Collection.
+ * @param collection the Collection to search
+ * @param type the type to look for
+ * @return a value of the given type found if there is a clear match,
+ * or <code>null</code> if none or more than one such value found
+ */
+ public static <T> T findValueOfType(Collection collection, Class<T> type) {
+ if (isEmpty(collection)) {
+ return null;
+ }
+ T value = null;
+ for (Object element : collection) {
+ if (type == null || type.isInstance(element)) {
+ if (value != null) {
+ // More than one value found... no clear single value.
+ return null;
+ }
+ value = (T) element;
+ }
+ }
+ return value;
+ }
+
+ /**
+ * Find a single value of one of the given types in the given Collection:
+ * searching the Collection for a value of the first type, then
+ * searching for a value of the second type, etc.
+ * @param collection the collection to search
+ * @param types the types to look for, in prioritized order
+ * @return a value of one of the given types found if there is a clear match,
+ * or <code>null</code> if none or more than one such value found
+ */
+ public static Object findValueOfType(Collection collection, Class[] types) {
+ if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {
+ return null;
+ }
+ for (Class type : types) {
+ Object value = findValueOfType(collection, type);
+ if (value != null) {
+ return value;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Determine whether the given Collection only contains a single unique object.
+ * @param collection the Collection to check
+ * @return <code>true</code> if the collection contains a single reference or
+ * multiple references to the same instance, <code>false</code> else
+ */
+ public static boolean hasUniqueObject(Collection collection) {
+ if (isEmpty(collection)) {
+ return false;
+ }
+ boolean hasCandidate = false;
+ Object candidate = null;
+ for (Iterator it = collection.iterator(); it.hasNext();) {
+ Object elem = it.next();
+ if (!hasCandidate) {
+ hasCandidate = true;
+ candidate = elem;
+ }
+ else if (candidate != elem) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Adapts an enumeration to an iterator.
+ * @param enumeration the enumeration
+ * @return the iterator
+ */
+ public static <E> Iterator<E> toIterator(Enumeration<E> enumeration) {
+ return new EnumerationIterator<E>(enumeration);
+ }
+
+ /**
+ * Iterator wrapping an Enumeration.
+ */
+ private static class EnumerationIterator<E> implements Iterator<E> {
+
+ private Enumeration<E> enumeration;
+
+ public EnumerationIterator(Enumeration<E> enumeration) {
+ this.enumeration = enumeration;
+ }
+
+ public boolean hasNext() {
+ return enumeration.hasMoreElements();
+ }
+
+ public E next() {
+ return enumeration.nextElement();
+ }
+
+ public void remove() throws UnsupportedOperationException {
+ throw new UnsupportedOperationException("Not supported");
+ }
+ }
+}
Propchange: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/CollectionUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/ObjectUtils.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/ObjectUtils.java?rev=889024&view=auto
==============================================================================
--- openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/ObjectUtils.java (added)
+++ openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/ObjectUtils.java Thu Dec 10 00:00:07 2009
@@ -0,0 +1,847 @@
+/*
+ * Copyright 2002-2009 the original author or authors.
+ *
+ * Licensed 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.openjpa.eclipse.util.pathmatch;
+
+import java.lang.reflect.Array;
+import java.util.Arrays;
+
+/**
+ * Miscellaneous object utility methods.
+ *
+ * <p>Mainly for internal use within the framework; consider
+ * <a href="http://jakarta.apache.org/commons/lang/">Jakarta's Commons Lang</a>
+ * for a more comprehensive suite of object utilities.
+ *
+ * <p>Thanks to Alex Ruiz for contributing several enhancements to this class!
+ *
+ * @author Juergen Hoeller
+ * @author Keith Donald
+ * @author Rod Johnson
+ * @author Rob Harrop
+ * @since 19.03.2004
+ * @see org.apache.commons.lang.ObjectUtils
+ */
+@SuppressWarnings("unchecked")
+public abstract class ObjectUtils {
+
+ private static final int INITIAL_HASH = 7;
+ private static final int MULTIPLIER = 31;
+
+ private static final String EMPTY_STRING = "";
+ private static final String NULL_STRING = "null";
+ private static final String ARRAY_START = "{";
+ private static final String ARRAY_END = "}";
+ private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
+ private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
+
+
+ /**
+ * Return whether the given throwable is a checked exception:
+ * that is, neither a RuntimeException nor an Error.
+ * @param ex the throwable to check
+ * @return whether the throwable is a checked exception
+ * @see java.lang.Exception
+ * @see java.lang.RuntimeException
+ * @see java.lang.Error
+ */
+ public static boolean isCheckedException(Throwable ex) {
+ return !(ex instanceof RuntimeException || ex instanceof Error);
+ }
+
+ /**
+ * Check whether the given exception is compatible with the exceptions
+ * declared in a throws clause.
+ * @param ex the exception to checked
+ * @param declaredExceptions the exceptions declared in the throws clause
+ * @return whether the given exception is compatible
+ */
+ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) {
+ if (!isCheckedException(ex)) {
+ return true;
+ }
+ if (declaredExceptions != null) {
+ int i = 0;
+ while (i < declaredExceptions.length) {
+ if (declaredExceptions[i].isAssignableFrom(ex.getClass())) {
+ return true;
+ }
+ i++;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Determine whether the given object is an array:
+ * either an Object array or a primitive array.
+ * @param obj the object to check
+ */
+ public static boolean isArray(Object obj) {
+ return (obj != null && obj.getClass().isArray());
+ }
+
+ /**
+ * Determine whether the given array is empty:
+ * i.e. <code>null</code> or of zero length.
+ * @param array the array to check
+ */
+ public static boolean isEmpty(Object[] array) {
+ return (array == null || array.length == 0);
+ }
+
+ /**
+ * Check whether the given array contains the given element.
+ * @param array the array to check (may be <code>null</code>,
+ * in which case the return value will always be <code>false</code>)
+ * @param element the element to check for
+ * @return whether the element has been found in the given array
+ */
+ public static boolean containsElement(Object[] array, Object element) {
+ if (array == null) {
+ return false;
+ }
+ for (Object arrayEle : array) {
+ if (nullSafeEquals(arrayEle, element)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Append the given Object to the given array, returning a new array
+ * consisting of the input array contents plus the given Object.
+ * @param array the array to append to (can be <code>null</code>)
+ * @param obj the Object to append
+ * @return the new array (of the same component type; never <code>null</code>)
+ */
+ public static Object[] addObjectToArray(Object[] array, Object obj) {
+ Class compType = Object.class;
+ if (array != null) {
+ compType = array.getClass().getComponentType();
+ }
+ else if (obj != null) {
+ compType = obj.getClass();
+ }
+ int newArrLength = (array != null ? array.length + 1 : 1);
+ Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
+ if (array != null) {
+ System.arraycopy(array, 0, newArr, 0, array.length);
+ }
+ newArr[newArr.length - 1] = obj;
+ return newArr;
+ }
+
+ /**
+ * Convert the given array (which may be a primitive array) to an
+ * object array (if necessary of primitive wrapper objects).
+ * <p>A <code>null</code> source value will be converted to an
+ * empty Object array.
+ * @param source the (potentially primitive) array
+ * @return the corresponding object array (never <code>null</code>)
+ * @throws IllegalArgumentException if the parameter is not an array
+ */
+ public static Object[] toObjectArray(Object source) {
+ if (source instanceof Object[]) {
+ return (Object[]) source;
+ }
+ if (source == null) {
+ return new Object[0];
+ }
+ if (!source.getClass().isArray()) {
+ throw new IllegalArgumentException("Source is not an array: " + source);
+ }
+ int length = Array.getLength(source);
+ if (length == 0) {
+ return new Object[0];
+ }
+ Class wrapperType = Array.get(source, 0).getClass();
+ Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
+ for (int i = 0; i < length; i++) {
+ newArray[i] = Array.get(source, i);
+ }
+ return newArray;
+ }
+
+
+ //---------------------------------------------------------------------
+ // Convenience methods for content-based equality/hash-code handling
+ //---------------------------------------------------------------------
+
+ /**
+ * Determine if the given objects are equal, returning <code>true</code>
+ * if both are <code>null</code> or <code>false</code> if only one is
+ * <code>null</code>.
+ * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality
+ * check based on the array elements rather than the array reference.
+ * @param o1 first Object to compare
+ * @param o2 second Object to compare
+ * @return whether the given objects are equal
+ * @see java.util.Arrays#equals
+ */
+ public static boolean nullSafeEquals(Object o1, Object o2) {
+ if (o1 == o2) {
+ return true;
+ }
+ if (o1 == null || o2 == null) {
+ return false;
+ }
+ if (o1.equals(o2)) {
+ return true;
+ }
+ if (o1.getClass().isArray() && o2.getClass().isArray()) {
+ if (o1 instanceof Object[] && o2 instanceof Object[]) {
+ return Arrays.equals((Object[]) o1, (Object[]) o2);
+ }
+ if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
+ return Arrays.equals((boolean[]) o1, (boolean[]) o2);
+ }
+ if (o1 instanceof byte[] && o2 instanceof byte[]) {
+ return Arrays.equals((byte[]) o1, (byte[]) o2);
+ }
+ if (o1 instanceof char[] && o2 instanceof char[]) {
+ return Arrays.equals((char[]) o1, (char[]) o2);
+ }
+ if (o1 instanceof double[] && o2 instanceof double[]) {
+ return Arrays.equals((double[]) o1, (double[]) o2);
+ }
+ if (o1 instanceof float[] && o2 instanceof float[]) {
+ return Arrays.equals((float[]) o1, (float[]) o2);
+ }
+ if (o1 instanceof int[] && o2 instanceof int[]) {
+ return Arrays.equals((int[]) o1, (int[]) o2);
+ }
+ if (o1 instanceof long[] && o2 instanceof long[]) {
+ return Arrays.equals((long[]) o1, (long[]) o2);
+ }
+ if (o1 instanceof short[] && o2 instanceof short[]) {
+ return Arrays.equals((short[]) o1, (short[]) o2);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Return as hash code for the given object; typically the value of
+ * <code>{@link Object#hashCode()}</code>. If the object is an array,
+ * this method will delegate to any of the <code>nullSafeHashCode</code>
+ * methods for arrays in this class. If the object is <code>null</code>,
+ * this method returns 0.
+ * @see #nullSafeHashCode(Object[])
+ * @see #nullSafeHashCode(boolean[])
+ * @see #nullSafeHashCode(byte[])
+ * @see #nullSafeHashCode(char[])
+ * @see #nullSafeHashCode(double[])
+ * @see #nullSafeHashCode(float[])
+ * @see #nullSafeHashCode(int[])
+ * @see #nullSafeHashCode(long[])
+ * @see #nullSafeHashCode(short[])
+ */
+ public static int nullSafeHashCode(Object obj) {
+ if (obj == null) {
+ return 0;
+ }
+ if (obj.getClass().isArray()) {
+ if (obj instanceof Object[]) {
+ return nullSafeHashCode((Object[]) obj);
+ }
+ if (obj instanceof boolean[]) {
+ return nullSafeHashCode((boolean[]) obj);
+ }
+ if (obj instanceof byte[]) {
+ return nullSafeHashCode((byte[]) obj);
+ }
+ if (obj instanceof char[]) {
+ return nullSafeHashCode((char[]) obj);
+ }
+ if (obj instanceof double[]) {
+ return nullSafeHashCode((double[]) obj);
+ }
+ if (obj instanceof float[]) {
+ return nullSafeHashCode((float[]) obj);
+ }
+ if (obj instanceof int[]) {
+ return nullSafeHashCode((int[]) obj);
+ }
+ if (obj instanceof long[]) {
+ return nullSafeHashCode((long[]) obj);
+ }
+ if (obj instanceof short[]) {
+ return nullSafeHashCode((short[]) obj);
+ }
+ }
+ return obj.hashCode();
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(Object[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);
+ }
+ return hash;
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(boolean[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + hashCode(array[i]);
+ }
+ return hash;
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(byte[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + array[i];
+ }
+ return hash;
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(char[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + array[i];
+ }
+ return hash;
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(double[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + hashCode(array[i]);
+ }
+ return hash;
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(float[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + hashCode(array[i]);
+ }
+ return hash;
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(int[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + array[i];
+ }
+ return hash;
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(long[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + hashCode(array[i]);
+ }
+ return hash;
+ }
+
+ /**
+ * Return a hash code based on the contents of the specified array.
+ * If <code>array</code> is <code>null</code>, this method returns 0.
+ */
+ public static int nullSafeHashCode(short[] array) {
+ if (array == null) {
+ return 0;
+ }
+ int hash = INITIAL_HASH;
+ int arraySize = array.length;
+ for (int i = 0; i < arraySize; i++) {
+ hash = MULTIPLIER * hash + array[i];
+ }
+ return hash;
+ }
+
+ /**
+ * Return the same value as <code>{@link Boolean#hashCode()}</code>.
+ * @see Boolean#hashCode()
+ */
+ public static int hashCode(boolean bool) {
+ return bool ? 1231 : 1237;
+ }
+
+ /**
+ * Return the same value as <code>{@link Double#hashCode()}</code>.
+ * @see Double#hashCode()
+ */
+ public static int hashCode(double dbl) {
+ long bits = Double.doubleToLongBits(dbl);
+ return hashCode(bits);
+ }
+
+ /**
+ * Return the same value as <code>{@link Float#hashCode()}</code>.
+ * @see Float#hashCode()
+ */
+ public static int hashCode(float flt) {
+ return Float.floatToIntBits(flt);
+ }
+
+ /**
+ * Return the same value as <code>{@link Long#hashCode()}</code>.
+ * @see Long#hashCode()
+ */
+ public static int hashCode(long lng) {
+ return (int) (lng ^ (lng >>> 32));
+ }
+
+
+ //---------------------------------------------------------------------
+ // Convenience methods for toString output
+ //---------------------------------------------------------------------
+
+ /**
+ * Return a String representation of an object's overall identity.
+ * @param obj the object (may be <code>null</code>)
+ * @return the object's identity as String representation,
+ * or an empty String if the object was <code>null</code>
+ */
+ public static String identityToString(Object obj) {
+ if (obj == null) {
+ return EMPTY_STRING;
+ }
+ return obj.getClass().getName() + "@" + getIdentityHexString(obj);
+ }
+
+ /**
+ * Return a hex String form of an object's identity hash code.
+ * @param obj the object
+ * @return the object's identity code in hex notation
+ */
+ public static String getIdentityHexString(Object obj) {
+ return Integer.toHexString(System.identityHashCode(obj));
+ }
+
+ /**
+ * Return a content-based String representation if <code>obj</code> is
+ * not <code>null</code>; otherwise returns an empty String.
+ * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
+ * an empty String rather than "null" for a <code>null</code> value.
+ * @param obj the object to build a display String for
+ * @return a display String representation of <code>obj</code>
+ * @see #nullSafeToString(Object)
+ */
+ public static String getDisplayString(Object obj) {
+ if (obj == null) {
+ return EMPTY_STRING;
+ }
+ return nullSafeToString(obj);
+ }
+
+ /**
+ * Determine the class name for the given object.
+ * <p>Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
+ * @param obj the object to introspect (may be <code>null</code>)
+ * @return the corresponding class name
+ */
+ public static String nullSafeClassName(Object obj) {
+ return (obj != null ? obj.getClass().getName() : NULL_STRING);
+ }
+
+ /**
+ * Return a String representation of the specified Object.
+ * <p>Builds a String representation of the contents in case of an array.
+ * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
+ * @param obj the object to build a String representation for
+ * @return a String representation of <code>obj</code>
+ */
+ public static String nullSafeToString(Object obj) {
+ if (obj == null) {
+ return NULL_STRING;
+ }
+ if (obj instanceof String) {
+ return (String) obj;
+ }
+ if (obj instanceof Object[]) {
+ return nullSafeToString((Object[]) obj);
+ }
+ if (obj instanceof boolean[]) {
+ return nullSafeToString((boolean[]) obj);
+ }
+ if (obj instanceof byte[]) {
+ return nullSafeToString((byte[]) obj);
+ }
+ if (obj instanceof char[]) {
+ return nullSafeToString((char[]) obj);
+ }
+ if (obj instanceof double[]) {
+ return nullSafeToString((double[]) obj);
+ }
+ if (obj instanceof float[]) {
+ return nullSafeToString((float[]) obj);
+ }
+ if (obj instanceof int[]) {
+ return nullSafeToString((int[]) obj);
+ }
+ if (obj instanceof long[]) {
+ return nullSafeToString((long[]) obj);
+ }
+ if (obj instanceof short[]) {
+ return nullSafeToString((short[]) obj);
+ }
+ String str = obj.toString();
+ return (str != null ? str : EMPTY_STRING);
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(Object[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+ sb.append(String.valueOf(array[i]));
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(boolean[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+
+ sb.append(array[i]);
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(byte[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+ sb.append(array[i]);
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(char[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+ sb.append("'").append(array[i]).append("'");
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(double[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+
+ sb.append(array[i]);
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(float[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+
+ sb.append(array[i]);
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(int[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+ sb.append(array[i]);
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(long[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+ sb.append(array[i]);
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+ /**
+ * Return a String representation of the contents of the specified array.
+ * <p>The String representation consists of a list of the array's elements,
+ * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
+ * by the characters <code>", "</code> (a comma followed by a space). Returns
+ * <code>"null"</code> if <code>array</code> is <code>null</code>.
+ * @param array the array to build a String representation for
+ * @return a String representation of <code>array</code>
+ */
+ public static String nullSafeToString(short[] array) {
+ if (array == null) {
+ return NULL_STRING;
+ }
+ int length = array.length;
+ if (length == 0) {
+ return EMPTY_ARRAY;
+ }
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++) {
+ if (i == 0) {
+ sb.append(ARRAY_START);
+ }
+ else {
+ sb.append(ARRAY_ELEMENT_SEPARATOR);
+ }
+ sb.append(array[i]);
+ }
+ sb.append(ARRAY_END);
+ return sb.toString();
+ }
+
+}
Propchange: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/ObjectUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/PathMatcher.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/PathMatcher.java?rev=889024&view=auto
==============================================================================
--- openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/PathMatcher.java (added)
+++ openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/PathMatcher.java Thu Dec 10 00:00:07 2009
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2002-2009 the original author or authors.
+ *
+ * Licensed 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.openjpa.eclipse.util.pathmatch;
+
+import java.util.Comparator;
+import java.util.Map;
+
+/**
+ * Strategy interface for <code>String</code>-based path matching.
+ *
+ * <p>Used by {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver},
+ * {@link org.springframework.web.servlet.handler.AbstractUrlHandlerMapping},
+ * {@link org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver},
+ * and {@link org.springframework.web.servlet.mvc.WebContentInterceptor}.
+ *
+ * <p>The default implementation is {@link AntPathMatcher}, supporting the
+ * Ant-style pattern syntax.
+ *
+ * @author Juergen Hoeller
+ * @since 1.2
+ * @see AntPathMatcher
+ */
+public interface PathMatcher {
+
+ /**
+ * Does the given <code>path</code> represent a pattern that can be matched
+ * by an implementation of this interface?
+ * <p>If the return value is <code>false</code>, then the {@link #match}
+ * method does not have to be used because direct equality comparisons
+ * on the static path Strings will lead to the same result.
+ * @param path the path String to check
+ * @return <code>true</code> if the given <code>path</code> represents a pattern
+ */
+ boolean isPattern(String path);
+
+ /**
+ * Match the given <code>path</code> against the given <code>pattern</code>,
+ * according to this PathMatcher's matching strategy.
+ * @param pattern the pattern to match against
+ * @param path the path String to test
+ * @return <code>true</code> if the supplied <code>path</code> matched,
+ * <code>false</code> if it didn't
+ */
+ boolean match(String pattern, String path);
+
+ /**
+ * Match the given <code>path</code> against the corresponding part of the given
+ * <code>pattern</code>, according to this PathMatcher's matching strategy.
+ * <p>Determines whether the pattern at least matches as far as the given base
+ * path goes, assuming that a full path may then match as well.
+ * @param pattern the pattern to match against
+ * @param path the path String to test
+ * @return <code>true</code> if the supplied <code>path</code> matched,
+ * <code>false</code> if it didn't
+ */
+ boolean matchStart(String pattern, String path);
+
+ /**
+ * Given a pattern and a full path, determine the pattern-mapped part.
+ * <p>This method is supposed to find out which part of the path is matched
+ * dynamically through an actual pattern, that is, it strips off a statically
+ * defined leading path from the given full path, returning only the actually
+ * pattern-matched part of the path.
+ * <p>For example: For "myroot/*.html" as pattern and "myroot/myfile.html"
+ * as full path, this method should return "myfile.html". The detailed
+ * determination rules are specified to this PathMatcher's matching strategy.
+ * <p>A simple implementation may return the given full path as-is in case
+ * of an actual pattern, and the empty String in case of the pattern not
+ * containing any dynamic parts (i.e. the <code>pattern</code> parameter being
+ * a static path that wouldn't qualify as an actual {@link #isPattern pattern}).
+ * A sophisticated implementation will differentiate between the static parts
+ * and the dynamic parts of the given path pattern.
+ * @param pattern the path pattern
+ * @param path the full path to introspect
+ * @return the pattern-mapped part of the given <code>path</code>
+ * (never <code>null</code>)
+ */
+ String extractPathWithinPattern(String pattern, String path);
+
+ /**
+ * Given a pattern and a full path, extract the URI template variables. URI template
+ * variables are expressed through curly brackets ('{' and '}').
+ * <p>For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will
+ * return a map containing "hotel"->"1".
+ *
+ * @param pattern the path pattern, possibly containing URI templates
+ * @param path the full path to extract template variables from
+ * @return a map, containing variable names as keys; variables values as values
+ */
+ Map<String, String> extractUriTemplateVariables(String pattern, String path);
+
+ /**
+ * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of explicitness for that path.
+ * <p>The full algorithm used depends on the underlying implementation, but generally, the returned
+ * <code>Comparator</code> will {@linkplain java.util.Collections#sort(java.util.List, java.util.Comparator) sort} a
+ * list so that more specific patterns (for that come before generic patterns.
+ *
+ * @param path the full path to use for comparison
+ * @return a comparator capable of sorting patterns in order of explicitness
+ */
+ Comparator<String> getPatternComparator(String path);
+
+ /**
+ * Combines two patterns into a new pattern that is returned.
+ * <p>The full algorithm used for combining the two pattern depends on the underlying implementation.
+ *
+ * @param pattern1 the first pattern
+ * @param pattern2 the second pattern
+ * @return the combination of the two patterns
+ * @throws IllegalArgumentException when the two patterns cannot be combined
+ */
+ String combine(String pattern1, String pattern2);
+}
Propchange: openjpa/trunk/contrib/devtools/org.apache.openjpa.eclipse/src/main/java/org/apache/openjpa/eclipse/util/pathmatch/PathMatcher.java
------------------------------------------------------------------------------
svn:eol-style = native