http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/eb7ec86e/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java index 8f1f7b6..a5f55b7 100644 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java +++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/InternalUtils.java @@ -17,7 +17,6 @@ package org.apache.tapestry5.ioc.internal.util; import org.apache.tapestry5.func.F; import org.apache.tapestry5.func.Mapper; import org.apache.tapestry5.func.Predicate; -import org.apache.tapestry5.internal.InternalBeanModelUtils; import org.apache.tapestry5.internal.plastic.PlasticInternalUtils; import org.apache.tapestry5.ioc.*; import org.apache.tapestry5.ioc.annotations.*; @@ -80,11 +79,10 @@ public class InternalUtils * * @param method * @return short string representation - * @deprecated use {@link InternalStringUtils#asString(Method)} instead. */ public static String asString(Method method) { - return InternalStringUtils.asString(method); + return InternalCommonsUtils.asString(method); } /** @@ -103,11 +101,10 @@ public class InternalUtils /** * Strips leading "_" and "$" and trailing "_" from the name. - * @deprecated use {@link InternalStringUtils#stripMemberName(String)} instead. */ public static String stripMemberName(String memberName) { - return InternalStringUtils.stripMemberName(memberName); + return InternalCommonsUtils.stripMemberName(memberName); } /** @@ -392,11 +389,10 @@ public class InternalUtils /** * Joins together some number of elements to form a comma separated list. - * @deprecated use {@link InternalStringUtils#join(List)} instead. */ public static String join(List elements) { - return InternalStringUtils.join(elements); + return InternalCommonsUtils.join(elements); } /** @@ -407,11 +403,10 @@ public class InternalUtils * objects to be joined together * @param separator * used between elements when joining - * @deprecated use {@link InternalStringUtils#asString(Method, String)} instead. */ public static String join(List elements, String separator) { - return InternalStringUtils.join(elements, separator); + return InternalCommonsUtils.join(elements, separator); } /** @@ -419,21 +414,19 @@ public class InternalUtils * * @return the elements converted to strings, sorted, joined with comma ... or "(none)" if the elements are null or * empty - * @deprecated use {@link InternalStringUtils#joinSorted(Collection)} instead. */ public static String joinSorted(Collection elements) { - return InternalStringUtils.joinSorted(elements); + return InternalCommonsUtils.joinSorted(elements); } /** * Returns true if the input is null, or is a zero length string (excluding leading/trailing whitespace). - * @deprecated use {@link InternalStringUtils#isBlank(String)} instead. */ public static boolean isBlank(String input) { - return InternalStringUtils.isBlank(input); + return InternalCommonsUtils.isBlank(input); } /** @@ -450,63 +443,27 @@ public class InternalUtils return false; } - /** - * @deprecated use {@link InternalStringUtils#isNonBlank(String)} instead. - */ public static boolean isNonBlank(String input) { - return InternalStringUtils.isNonBlank(input); + return InternalCommonsUtils.isNonBlank(input); } /** * Capitalizes a string, converting the first character to uppercase. - * @deprecated use {@link InternalStringUtils#capitalize(String)} instead. */ public static String capitalize(String input) { - return InternalStringUtils.capitalize(input); + return InternalCommonsUtils.capitalize(input); } /** * Sniffs the object to see if it is a {@link Location} or {@link Locatable}. Returns null if null or not * convertable to a location. */ - + public static Location locationOf(Object location) { - if (location == null) - return null; - - if (location instanceof Location) - return (Location) location; - - if (location instanceof Locatable) - return ((Locatable) location).getLocation(); - - return null; - } - - /** - * Extracts the string keys from a map and returns them in sorted order. The keys are converted to strings. - * - * @param map - * the map to extract keys from (may be null) - * @return the sorted keys, or the empty set if map is null - */ - - public static List<String> sortedKeys(Map map) - { - if (map == null) - return Collections.emptyList(); - - List<String> keys = CollectionFactory.newList(); - - for (Object o : map.keySet()) - keys.add(String.valueOf(o)); - - Collections.sort(keys); - - return keys; + return InternalCommonsUtils.locationOf(location); } public static <K, V> Set<K> keys(Map<K, V> map) @@ -572,11 +529,10 @@ public class InternalUtils /** * Return true if the input string contains the marker for symbols that must be expanded. - * @deprecated use {@link InternalStringUtils#containsSymbols(String)} instead. */ public static boolean containsSymbols(String input) { - return InternalStringUtils.containsSymbols(input); + return InternalCommonsUtils.containsSymbols(input); } /** @@ -584,11 +540,10 @@ public class InternalUtils * generally a fully qualified class name, though tapestry-core also uses this method for the occasional property * expression (which is also dot separated). Returns the input string unchanged if it does not contain a period * character. - * @deprecated use {@link InternalStringUtils#lastTerm(String)} instead. */ public static String lastTerm(String input) { - return InternalStringUtils.lastTerm(input); + return InternalCommonsUtils.lastTerm(input); } /** @@ -684,15 +639,7 @@ public class InternalUtils */ public static <K, V> void addToMapList(Map<K, List<V>> map, K key, V value) { - List<V> list = map.get(key); - - if (list == null) - { - list = CollectionFactory.newList(); - map.put(key, list); - } - - list.add(value); + InternalCommonsUtils.addToMapList(map, key, value); } /** @@ -777,7 +724,7 @@ public class InternalUtils */ public static AnnotationProvider toAnnotationProvider(final Class element) { - return InternalBeanModelUtils.toAnnotationProvider(element); + return InternalCommonsUtils.toAnnotationProvider(element); } /** @@ -1392,7 +1339,7 @@ public class InternalUtils { String value = namedAnnotation.value(); - if (InternalUtils.isNonBlank(value)) + if (InternalCommonsUtils.isNonBlank(value)) { return value; } @@ -1404,7 +1351,7 @@ public class InternalUtils public static AnnotationProvider toAnnotationProvider(final Method element) { - return InternalBeanModelUtils.toAnnotationProvider(element); + return InternalCommonsUtils.toAnnotationProvider(element); } public static <T> ObjectCreator<T> createConstructorConstructionPlan(final OperationTracker tracker, final ObjectLocator locator, @@ -1567,7 +1514,7 @@ public class InternalUtils @Override public void run() { - final ObjectCreator[] parameters = InternalUtils.calculateParametersForMethod(method, locator, + final ObjectCreator[] parameters = calculateParametersForMethod(method, locator, resources, tracker); plan.add(new InitializationPlan<Object>() @@ -1651,4 +1598,55 @@ public class InternalUtils { return F.flow(creators).map(CREATE_OBJECT).toArray(Object.class); } + + /** + * Extracts the string keys from a map and returns them in sorted order. The keys are converted to strings. + * + * @param map + * the map to extract keys from (may be null) + * @return the sorted keys, or the empty set if map is null + */ + + public static List<String> sortedKeys(Map map) + { + return InternalCommonsUtils.sortedKeys(map); + } + + /** + * Capitalizes the string, and inserts a space before each upper case character (or sequence of upper case + * characters). Thus "userId" becomes "User Id", etc. Also, converts underscore into space (and capitalizes the + * following word), thus "user_id" also becomes "User Id". + */ + public static String toUserPresentable(String id) + { + return InternalCommonsUtils.toUserPresentable(id); + } + + /** + * Used to convert a property expression into a key that can be used to locate various resources (Blocks, messages, + * etc.). Strips out any punctuation characters, leaving just words characters (letters, number and the + * underscore). + * + * @param expression a property expression + * @return the expression with punctuation removed + */ + public static String extractIdFromPropertyExpression(String expression) + { + return InternalCommonsUtils.extractIdFromPropertyExpression(expression); + } + + /** + * Looks for a label within the messages based on the id. If found, it is used, otherwise the name is converted to a + * user presentable form. + */ + public static String defaultLabel(String id, Messages messages, String propertyExpression) + { + return InternalCommonsUtils.defaultLabel(id, messages, propertyExpression); + } + + public static String replace(String input, Pattern pattern, String replacement) + { + return InternalCommonsUtils.replace(input, pattern, replacement); + } + }
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/eb7ec86e/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/LockSupport.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/LockSupport.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/LockSupport.java deleted file mode 100644 index 41de363..0000000 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/LockSupport.java +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2012 The Apache Software Foundation -// -// 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.tapestry5.ioc.internal.util; - -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantReadWriteLock; - -/** - * Base class for classes that need to manage a ReadWriteLock. - */ -public abstract class LockSupport -{ - private final Lock readLock, writeLock; - - protected LockSupport() - { - ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - - readLock = lock.readLock(); - writeLock = lock.writeLock(); - } - - /** - * Locks the shared read lock. Any number of threads may lock the read lock at the same time. - */ - protected final void acquireReadLock() - { - readLock.lock(); - } - - /** - * Takes the exclusive write lock. Once started, no other thread lock the read or write lock. When this method returns, - * this thread will have locked the write lock and no other thread will have either the read or write lock. - * Note that this thread must first drop the read lock (if it has it) before attempting to take the write lock, or this method will block forever. - */ - protected final void takeWriteLock() - { - writeLock.lock(); - } - - /** - * Releases the shared read lock. - */ - protected final void releaseReadLock() - { - readLock.unlock(); - } - - /** - * Releases the exclusive read lock. - */ - protected final void releaseWriteLock() - { - writeLock.unlock(); - } - - /** - * Releases the read lock, then takes the write lock. There's a short window where the thread will have neither lock: - * during that window, some other thread may have a chance to take the write lock. In code, you'll often see a second check - * inside the code that has the write lock to see if the update to perform is still necessary. - */ - protected final void upgradeReadLockToWriteLock() - { - releaseReadLock(); - // This is that instant where another thread may grab the write lock. Very rare, but possible. - takeWriteLock(); - } - - /** - * Takes the read lock then releases the write lock. - */ - protected final void downgradeWriteLockToReadLock() - { - acquireReadLock(); - releaseWriteLock(); - } -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/eb7ec86e/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/MessageFormatterImpl.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/MessageFormatterImpl.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/MessageFormatterImpl.java deleted file mode 100644 index f4d8949..0000000 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/MessageFormatterImpl.java +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2006-2013 The Apache Software Foundation -// -// 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.tapestry5.ioc.internal.util; - -import org.apache.tapestry5.ioc.MessageFormatter; -import org.apache.tapestry5.ioc.util.ExceptionUtils; - -import java.util.Locale; - - -public class MessageFormatterImpl implements MessageFormatter -{ - private final String format; - - private final Locale locale; - - public MessageFormatterImpl(String format, Locale locale) - { - this.format = format; - this.locale = locale; - } - - @Override - public String format(Object... args) - { - for (int i = 0; i < args.length; i++) - { - Object arg = args[i]; - - if (Throwable.class.isInstance(arg)) - { - args[i] = ExceptionUtils.toMessage((Throwable) arg); - } - } - - // Might be tempting to create a Formatter object and just keep reusing it ... but - // Formatters are not threadsafe. - - return String.format(locale, format, args); - } - - /** - * Returns the underlying format string for this formatter. - * - * @since 5.4 - */ - @Override - public String toString() - { - return format; - } - -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/eb7ec86e/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/MessagesImpl.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/MessagesImpl.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/MessagesImpl.java deleted file mode 100644 index c06ef90..0000000 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/MessagesImpl.java +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2006, 2012 The Apache Software Foundation -// -// 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.tapestry5.ioc.internal.util; - -import org.apache.tapestry5.ioc.Messages; -import org.apache.tapestry5.ioc.util.AbstractMessages; - -import java.util.*; - -/** - * Implementation of {@link org.apache.tapestry5.ioc.Messages} based around a {@link java.util.ResourceBundle}. - */ -public class MessagesImpl extends AbstractMessages -{ - private final Map<String, String> properties = CollectionFactory.newCaseInsensitiveMap(); - - /** - * Finds the messages for a given Messages utility class. Strings the trailing "Messages" and replaces it with - * "Strings" to form the base path. Loads the bundle using the default locale, and the class' class loader. - * - * @param forClass - * @return Messages for the class - */ - public static Messages forClass(Class forClass) - { - String className = forClass.getName(); - String stringsClassName = className.replaceAll("Messages$", "Strings"); - - Locale locale = Locale.getDefault(); - - ResourceBundle bundle = ResourceBundle.getBundle(stringsClassName, locale, forClass.getClassLoader()); - - return new MessagesImpl(locale, bundle); - } - - public MessagesImpl(Locale locale, ResourceBundle bundle) - { - super(locale); - - // Our best (threadsafe) chance to determine all the available keys. - Enumeration<String> e = bundle.getKeys(); - while (e.hasMoreElements()) - { - String key = e.nextElement(); - String value = bundle.getString(key); - - properties.put(key, value); - } - } - - @Override - protected String valueForKey(String key) - { - return properties.get(key); - } - - @Override - public Set<String> getKeys() - { - return properties.keySet(); - } -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/eb7ec86e/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java index a9af358..6d13d66 100644 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java +++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java @@ -17,6 +17,7 @@ package org.apache.tapestry5.ioc.modules; import org.apache.tapestry5.func.Flow; import org.apache.tapestry5.ioc.*; import org.apache.tapestry5.ioc.annotations.*; +import org.apache.tapestry5.ioc.internal.BasicTypeCoercions; import org.apache.tapestry5.ioc.internal.services.*; import org.apache.tapestry5.ioc.internal.services.cron.PeriodicExecutorImpl; import org.apache.tapestry5.ioc.internal.util.CollectionFactory; @@ -189,301 +190,9 @@ public final class TapestryIOCModule @Contribute(TypeCoercer.class) public static void provideBasicTypeCoercions(Configuration<CoercionTuple> configuration) { - add(configuration, Object.class, String.class, new Coercion<Object, String>() - { - @Override - public String coerce(Object input) - { - return input.toString(); - } - }); - - add(configuration, Object.class, Boolean.class, new Coercion<Object, Boolean>() - { - @Override - public Boolean coerce(Object input) - { - return input != null; - } - }); - - add(configuration, String.class, Double.class, new Coercion<String, Double>() - { - @Override - public Double coerce(String input) - { - return new Double(input); - } - }); - - // String to BigDecimal is important, as String->Double->BigDecimal would lose - // precision. - - add(configuration, String.class, BigDecimal.class, new Coercion<String, BigDecimal>() - { - @Override - public BigDecimal coerce(String input) - { - return new BigDecimal(input); - } - }); - - add(configuration, BigDecimal.class, Double.class, new Coercion<BigDecimal, Double>() - { - @Override - public Double coerce(BigDecimal input) - { - return input.doubleValue(); - } - }); - - add(configuration, String.class, BigInteger.class, new Coercion<String, BigInteger>() - { - @Override - public BigInteger coerce(String input) - { - return new BigInteger(input); - } - }); - - add(configuration, String.class, Long.class, new Coercion<String, Long>() - { - @Override - public Long coerce(String input) - { - return new Long(input); - } - }); - - add(configuration, Long.class, Byte.class, new Coercion<Long, Byte>() - { - @Override - public Byte coerce(Long input) - { - return input.byteValue(); - } - }); - - add(configuration, Long.class, Short.class, new Coercion<Long, Short>() - { - @Override - public Short coerce(Long input) - { - return input.shortValue(); - } - }); - - add(configuration, Long.class, Integer.class, new Coercion<Long, Integer>() - { - @Override - public Integer coerce(Long input) - { - return input.intValue(); - } - }); - - add(configuration, Number.class, Long.class, new Coercion<Number, Long>() - { - @Override - public Long coerce(Number input) - { - return input.longValue(); - } - }); - - add(configuration, Double.class, Float.class, new Coercion<Double, Float>() - { - @Override - public Float coerce(Double input) - { - return input.floatValue(); - } - }); - - add(configuration, Long.class, Double.class, new Coercion<Long, Double>() - { - @Override - public Double coerce(Long input) - { - return input.doubleValue(); - } - }); - - add(configuration, String.class, Boolean.class, new Coercion<String, Boolean>() - { - @Override - public Boolean coerce(String input) - { - String trimmed = input == null ? "" : input.trim(); - - if (trimmed.equalsIgnoreCase("false") || trimmed.length() == 0) - return false; - - // Any non-blank string but "false" - - return true; - } - }); - - add(configuration, Number.class, Boolean.class, new Coercion<Number, Boolean>() - { - @Override - public Boolean coerce(Number input) - { - return input.longValue() != 0; - } - }); - - add(configuration, Void.class, Boolean.class, new Coercion<Void, Boolean>() - { - @Override - public Boolean coerce(Void input) - { - return false; - } - }); - - add(configuration, Collection.class, Boolean.class, new Coercion<Collection, Boolean>() - { - @Override - public Boolean coerce(Collection input) - { - return !input.isEmpty(); - } - }); - - add(configuration, Object.class, List.class, new Coercion<Object, List>() - { - @Override - public List coerce(Object input) - { - return Collections.singletonList(input); - } - }); - - add(configuration, Object[].class, List.class, new Coercion<Object[], List>() - { - @Override - public List coerce(Object[] input) - { - return Arrays.asList(input); - } - }); - - add(configuration, Object[].class, Boolean.class, new Coercion<Object[], Boolean>() - { - @Override - public Boolean coerce(Object[] input) - { - return input != null && input.length > 0; - } - }); - - add(configuration, Float.class, Double.class, new Coercion<Float, Double>() - { - @Override - public Double coerce(Float input) - { - return input.doubleValue(); - } - }); - - Coercion primitiveArrayCoercion = new Coercion<Object, List>() - { - @Override - public List<Object> coerce(Object input) - { - int length = Array.getLength(input); - Object[] array = new Object[length]; - for (int i = 0; i < length; i++) - { - array[i] = Array.get(input, i); - } - return Arrays.asList(array); - } - }; - - add(configuration, byte[].class, List.class, primitiveArrayCoercion); - add(configuration, short[].class, List.class, primitiveArrayCoercion); - add(configuration, int[].class, List.class, primitiveArrayCoercion); - add(configuration, long[].class, List.class, primitiveArrayCoercion); - add(configuration, float[].class, List.class, primitiveArrayCoercion); - add(configuration, double[].class, List.class, primitiveArrayCoercion); - add(configuration, char[].class, List.class, primitiveArrayCoercion); - add(configuration, boolean[].class, List.class, primitiveArrayCoercion); - - add(configuration, String.class, File.class, new Coercion<String, File>() - { - @Override - public File coerce(String input) - { - return new File(input); - } - }); - - add(configuration, String.class, TimeInterval.class, new Coercion<String, TimeInterval>() - { - @Override - public TimeInterval coerce(String input) - { - return new TimeInterval(input); - } - }); - - add(configuration, TimeInterval.class, Long.class, new Coercion<TimeInterval, Long>() - { - @Override - public Long coerce(TimeInterval input) - { - return input.milliseconds(); - } - }); - - add(configuration, Object.class, Object[].class, new Coercion<Object, Object[]>() - { - @Override - public Object[] coerce(Object input) - { - return new Object[] - {input}; - } - }); - - add(configuration, Collection.class, Object[].class, new Coercion<Collection, Object[]>() - { - @Override - public Object[] coerce(Collection input) - { - return input.toArray(); - } - }); - - add(configuration, Flow.class, List.class, new Coercion<Flow, List>() - { - @Override - public List coerce(Flow input) - { - return input.toList(); - } - }); - - add(configuration, Flow.class, Boolean.class, new Coercion<Flow, Boolean>() - { - @Override - public Boolean coerce(Flow input) - { - return !input.isEmpty(); - } - }); - + BasicTypeCoercions.provideBasicTypeCoercions(configuration); } - - private static <S, T> void add(Configuration<CoercionTuple> configuration, Class<S> sourceType, - Class<T> targetType, Coercion<S, T> coercion) - { - configuration.add(CoercionTuple.create(sourceType, targetType, coercion)); - } - + /** * <dl> * <dt>SystemProperties</dt> http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/eb7ec86e/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/AbstractMessages.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/AbstractMessages.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/AbstractMessages.java deleted file mode 100644 index 590c337..0000000 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/AbstractMessages.java +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright 2006, 2009, 2011 The Apache Software Foundation -// -// 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.tapestry5.ioc.util; - -import org.apache.tapestry5.ioc.MessageFormatter; -import org.apache.tapestry5.ioc.Messages; -import org.apache.tapestry5.ioc.internal.util.CollectionFactory; -import org.apache.tapestry5.ioc.internal.util.MessageFormatterImpl; - -import java.util.Locale; -import java.util.Map; - -/** - * Abstract implementation of {@link Messages} that doesn't know where values come from (that information is supplied in - * a subclass, via the {@link #valueForKey(String)} method). - */ -public abstract class AbstractMessages implements Messages -{ - /** - * String key to MF instance. - */ - private final Map<String, MessageFormatter> cache = CollectionFactory.newConcurrentMap(); - - private final Locale locale; - - protected AbstractMessages(Locale locale) - { - this.locale = locale; - } - - /** - * Invoked to provide the value for a particular key. This may be invoked multiple times even for the same key. The - * implementation should <em>ignore the case of the key</em>. - * - * @param key the key to obtain a value for (case insensitive) - * @return the value for the key, or null if this instance can not provide the value - */ - protected abstract String valueForKey(String key); - - - @Override - public boolean contains(String key) - { - return valueForKey(key) != null; - } - - @Override - public String get(String key) - { - if (contains(key)) return valueForKey(key); - - return String.format("[[missing key: %s]]", key); - } - - @Override - public MessageFormatter getFormatter(String key) - { - MessageFormatter result = cache.get(key); - - if (result == null) - { - result = buildMessageFormatter(key); - cache.put(key, result); - } - - return result; - } - - private MessageFormatter buildMessageFormatter(String key) - { - String format = get(key); - - return new MessageFormatterImpl(format, locale); - } - - @Override - public String format(String key, Object... args) - { - return getFormatter(key).format(args); - } - -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/eb7ec86e/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/TimeInterval.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/TimeInterval.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/TimeInterval.java deleted file mode 100644 index eaffe46..0000000 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/TimeInterval.java +++ /dev/null @@ -1,195 +0,0 @@ -// Copyright 2007, 2008, 2010, 2011 The Apache Software Foundation -// -// 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.tapestry5.ioc.util; - -import org.apache.tapestry5.ioc.internal.util.CollectionFactory; -import org.apache.tapestry5.ioc.internal.util.InternalUtils; - -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Used to represent a period of time, specifically as a configuration value. This is often used to specify timeouts. - * <p/> - * TimePeriods are parsed from strings. - * <p/> - * The string specifys a number of terms. The values of all the terms are summed together to form the total time period. - * Each term consists of a number followed by a unit. Units (from largest to smallest) are: <dl> <dt>y <dd>year <dt>d - * <dd>day <dt>h <dd>hour <dt>m <dd>minute <dt>s <dd>second <dt>ms <dd>millisecond </dl> <p> Example: "2 h 30 m". By - * convention, terms are specified largest to smallest. A term without a unit is assumed to be milliseconds. Units are - * case insensitive ("h" or "H" are treated the same). - */ -public class TimeInterval -{ - private static final Map<String, Long> UNITS = CollectionFactory.newCaseInsensitiveMap(); - - private static final long MILLISECOND = 1000l; - - static - { - UNITS.put("ms", 1l); - UNITS.put("s", MILLISECOND); - UNITS.put("m", 60 * MILLISECOND); - UNITS.put("h", 60 * UNITS.get("m")); - UNITS.put("d", 24 * UNITS.get("h")); - UNITS.put("y", 365 * UNITS.get("d")); - } - - /** - * The unit keys, sorted in descending order. - */ - private static final String[] UNIT_KEYS = - { "y", "d", "h", "m", "s", "ms" }; - - private static final Pattern PATTERN = Pattern.compile("\\s*(\\d+)\\s*([a-z]*)", Pattern.CASE_INSENSITIVE); - - private final long milliseconds; - - /** - * Creates a TimeInterval for a string. - * - * @param input - * the string specifying the amount of time in the period - */ - public TimeInterval(String input) - { - this(parseMilliseconds(input)); - } - - public TimeInterval(long milliseconds) - { - this.milliseconds = milliseconds; - } - - public long milliseconds() - { - return milliseconds; - } - - public long seconds() - { - return milliseconds / MILLISECOND; - } - - /** - * Converts the milliseconds back into a string (compatible with {@link #TimeInterval(String)}). - * - * @since 5.2.0 - */ - public String toDescription() - { - StringBuilder builder = new StringBuilder(); - - String sep = ""; - - long remainder = milliseconds; - - for (String key : UNIT_KEYS) - { - if (remainder == 0) - break; - - long value = UNITS.get(key); - - long units = remainder / value; - - if (units > 0) - { - builder.append(sep); - builder.append(units); - builder.append(key); - - sep = " "; - - remainder = remainder % value; - } - } - - return builder.toString(); - } - - static long parseMilliseconds(String input) - { - long milliseconds = 0l; - - Matcher matcher = PATTERN.matcher(input); - - matcher.useAnchoringBounds(true); - - // TODO: Notice non matching characters and reject input, including at end - - int lastMatchEnd = -1; - - while (matcher.find()) - { - int start = matcher.start(); - - if (lastMatchEnd + 1 < start) - { - String invalid = input.substring(lastMatchEnd + 1, start); - throw new RuntimeException(String.format("Unexpected string '%s' (in time interval '%s').", invalid, input)); - } - - lastMatchEnd = matcher.end(); - - long count = Long.parseLong(matcher.group(1)); - String units = matcher.group(2); - - if (units.length() == 0) - { - milliseconds += count; - continue; - } - - Long unitValue = UNITS.get(units); - - if (unitValue == null) - throw new RuntimeException(String.format("Unknown time interval unit '%s' (in '%s'). Defined units: %s.", units, input, InternalUtils.joinSorted(UNITS.keySet()))); - - milliseconds += count * unitValue; - } - - if (lastMatchEnd + 1 < input.length()) - { - String invalid = input.substring(lastMatchEnd + 1); - throw new RuntimeException(String.format("Unexpected string '%s' (in time interval '%s').", invalid, input)); - } - - return milliseconds; - } - - @Override - public String toString() - { - return String.format("TimeInterval[%d ms]", milliseconds); - } - - @Override - public boolean equals(Object obj) - { - if (obj == null) - return false; - - if (obj instanceof TimeInterval) - { - TimeInterval tp = (TimeInterval) obj; - - return milliseconds == tp.milliseconds; - } - - return false; - } -} http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/eb7ec86e/tapestry-ioc/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java ---------------------------------------------------------------------- diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java deleted file mode 100644 index 77c9a8d..0000000 --- a/tapestry-ioc/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright 2007, 2008, 2010, 2011 The Apache Software Foundation -// -// 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.tapestry5.util; - -import java.util.Map; - -import org.apache.tapestry5.ioc.internal.util.CollectionFactory; -import org.apache.tapestry5.ioc.internal.util.InternalUtils; -import org.apache.tapestry5.ioc.services.Coercion; -import org.apache.tapestry5.ioc.util.AvailableValues; -import org.apache.tapestry5.ioc.util.UnknownValueException; - -/** - * A {@link org.apache.tapestry5.ioc.services.Coercion} for converting strings into an instance of a particular - * enumerated type. The {@link Enum#name() name} is used as the key to identify the enum instance, in a case-insensitive - * fashion. - * <p> - * Moved from tapestry-core to tapestry-ioc is release 5.3, but kept in same package for compatibility. - * - * @param <T> - * the type of enumeration - */ -public final class StringToEnumCoercion<T extends Enum> implements Coercion<String, T> -{ - private final Class<T> enumClass; - - private final Map<String, T> stringToEnum = CollectionFactory.newCaseInsensitiveMap(); - - public StringToEnumCoercion(Class<T> enumClass) - { - this(enumClass, enumClass.getEnumConstants()); - } - - public StringToEnumCoercion(Class<T> enumClass, T... values) - { - this.enumClass = enumClass; - - for (T value : values) - stringToEnum.put(value.name(), value); - } - - @Override - public T coerce(String input) - { - if (InternalUtils.isBlank(input)) - return null; - - T result = stringToEnum.get(input); - - if (result == null) - { - String message = String.format("Input '%s' does not identify a value from enumerated type %s.", input, - enumClass.getName()); - - throw new UnknownValueException(message, new AvailableValues(enumClass.getName() + " enum constants", - stringToEnum)); - } - - return result; - } - - /** - * Allows an alias value (alternate) string to reference a value. - * - * @since 5.2.2 - */ - public StringToEnumCoercion<T> addAlias(String alias, T value) - { - stringToEnum.put(alias, value); - - return this; - } - - public static <T extends Enum> StringToEnumCoercion<T> create(Class<T> enumClass) - { - return new StringToEnumCoercion<T>(enumClass); - } - -}