You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by ahgittin <gi...@git.apache.org> on 2016/06/26 21:02:11 UTC

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

GitHub user ahgittin opened a pull request:

    https://github.com/apache/brooklyn-server/pull/221

    Misc cleanup of javalang, type coercion, jsonya, and Maybe

    big tidy is https://github.com/apache/brooklyn-server/commit/ec4da197d5caf3ac9bb63ab94924718aae83c615 moving most of our static `TypeCoercions` to a non-static `utils` impl (but not changing how we use `TypeCoercions` ... apart from the exception it throws)
    
    others are as described in the individual commits, nothing too exciting, and big improvements to tests and javadocs
    
    encountered while looking at how we can improve yaml type inference

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/ahgittin/brooklyn-server misc-cleanup-javalang-and-type-coercion

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/brooklyn-server/pull/221.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #221
    
----
commit 1f973df594cd359d1ad04c52c4f003d18191b339
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Date:   2016-06-24T09:58:38Z

    Maybe gives more control over the exceptions it throws
    
    You can do `orThrowingUnwrapped()` to suppress the add'l caller trace,
    and you can change more easily change the type of the exception it will throw.

commit 785342a4aebe0af47fac1e44205f85c28d320f79
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Date:   2016-06-24T21:48:10Z

    Maybe offers more clarity around whether it creates an exception on Absent
    
    and has a toOptional, and tests

commit 864400306d0433e26a51e96639de2d2b08b9bc4f
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Date:   2016-06-24T10:00:14Z

    Jsonya allows primitives and lists at the root now
    
    bringing it in line with json which has always allowed lists and now allows primitives, as root objects

commit ec4da197d5caf3ac9bb63ab94924718aae83c615
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Date:   2016-06-24T10:02:52Z

    refactor TypeCoercions so that most is in utils with an interface
    
    with the Brooklyn TypeCoercions referring to and extending that

commit 23ab1c506bdda9a3ac79199146ea9750d18d9005
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Date:   2016-06-24T10:13:18Z

    tidy/enhancements to javalang utils
    
    a few more methods which were elsewhere or not present,
    better methods using Maybe, and callers updated to use them

----


---
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.
---

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

Posted by ahgittin <gi...@git.apache.org>.
Github user ahgittin commented on the issue:

    https://github.com/apache/brooklyn-server/pull/221
  
    good idea @grkvlt , done


---
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.
---

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

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on the issue:

    https://github.com/apache/brooklyn-server/pull/221
  
    @ahgittin Nice, looks like a good refactor. Minor comment about singletons...?


---
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.
---

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

Posted by grkvlt <gi...@git.apache.org>.
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.
---

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

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/brooklyn-server/pull/221


---
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.
---