You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by grkvlt <gi...@git.apache.org> on 2016/07/03 07:33:00 UTC

[GitHub] brooklyn-server pull request #221: Misc cleanup of javalang, type coercion, ...

Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/221#discussion_r69386962
  
    --- Diff: core/src/main/java/org/apache/brooklyn/util/core/flags/TypeCoercions.java ---
    @@ -53,443 +36,177 @@
     import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
     import org.apache.brooklyn.core.sensor.Sensors;
     import org.apache.brooklyn.util.JavaGroovyEquivalents;
    -import org.apache.brooklyn.util.collections.MutableSet;
    -import org.apache.brooklyn.util.collections.QuorumCheck;
    -import org.apache.brooklyn.util.collections.QuorumCheck.QuorumChecks;
     import org.apache.brooklyn.util.core.task.Tasks;
    -import org.apache.brooklyn.util.exceptions.Exceptions;
     import org.apache.brooklyn.util.guava.Maybe;
    -import org.apache.brooklyn.util.javalang.Enums;
    +import org.apache.brooklyn.util.javalang.Boxing;
     import org.apache.brooklyn.util.javalang.JavaClassNames;
    -import org.apache.brooklyn.util.net.Cidr;
    -import org.apache.brooklyn.util.net.Networking;
    -import org.apache.brooklyn.util.net.UserAndHostAndPort;
    -import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes;
    -import org.apache.brooklyn.util.text.Strings;
    -import org.apache.brooklyn.util.time.Duration;
    -import org.apache.brooklyn.util.time.Time;
    -import org.apache.brooklyn.util.yaml.Yamls;
    +import org.apache.brooklyn.util.javalang.Reflections;
    +import org.apache.brooklyn.util.javalang.coerce.CommonAdaptorTypeCoercions;
    +import org.apache.brooklyn.util.javalang.coerce.EnumTypeCoercions;
    +import org.apache.brooklyn.util.javalang.coerce.PrimitiveStringTypeCoercions;
    +import org.apache.brooklyn.util.javalang.coerce.TypeCoercer;
    +import org.apache.brooklyn.util.javalang.coerce.TypeCoercerExtensible;
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     
    -import com.google.common.base.CaseFormat;
     import com.google.common.base.Function;
    -import com.google.common.base.Objects;
    -import com.google.common.base.Preconditions;
     import com.google.common.base.Predicate;
    -import com.google.common.collect.HashBasedTable;
    -import com.google.common.collect.ImmutableList;
     import com.google.common.collect.ImmutableMap;
    -import com.google.common.collect.Iterables;
    -import com.google.common.collect.Lists;
    -import com.google.common.collect.Maps;
    -import com.google.common.collect.Sets;
    -import com.google.common.collect.Table;
    -import com.google.common.net.HostAndPort;
    -import com.google.common.primitives.Primitives;
     import com.google.common.reflect.TypeToken;
     
    -@SuppressWarnings("rawtypes")
    +/** Static class providing a shared {@link TypeCoercer} for all of Brooklyn */
     public class TypeCoercions {
     
         private static final Logger log = LoggerFactory.getLogger(TypeCoercions.class);
         
         private TypeCoercions() {}
     
    -    /** Store the coercion {@link Function functions} in a {@link Table table}. */
    -    @GuardedBy("TypeCoercions.class")
    -    private static Table<Class, Class, Function> registry = HashBasedTable.create();
    -
    -    /**
    -     * Attempts to coerce {@code value} to {@code targetType}.
    -     * <p>
    -     * Maintains a registry of adapter functions for type pairs in a {@link Table} which
    -     * is searched after checking various strategies, including the following:
    -     * <ul>
    -     * <li>{@code value.asTargetType()}
    -     * <li>{@code TargetType.fromType(value)} (if {@code value instanceof Type})
    -     * <li>{@code value.targetTypeValue()} (handy for primitives)
    -     * <li>{@code TargetType.valueOf(value)} (for enums)
    -     * </ul>
    -     * <p>
    -     * A default set of adapters will handle most common Java-type coercions
    -     * as well as <code>String</code> coercion to:
    -     * <ul>
    -     * <li> {@link Set}, {@link List}, {@link Map} and similar -- parses as YAML
    -     * <li> {@link Date} -- parses using {@link Time#parseDate(String)}
    -     * <li> {@link Duration} -- parses using {@link Duration#parse(String)}
    -     * </ul>
    -     */
    -    public static <T> T coerce(Object value, Class<T> targetType) {
    -        return coerce(value, TypeToken.of(targetType));
    +    static TypeCoercerExtensible coercer;
    --- End diff --
    
    I'm a bit wary of having our singleton instance as a non-final package scoped field here, is `private static final` better?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---