private static void join()

in scripts/src/main/java/com/gu/typesafe/config/impl/ConfigConcatenation.java [91:140]


    private static void join(ArrayList<com.gu.typesafe.config.impl.AbstractConfigValue> builder, com.gu.typesafe.config.impl.AbstractConfigValue origRight) {
        com.gu.typesafe.config.impl.AbstractConfigValue left = builder.get(builder.size() - 1);
        com.gu.typesafe.config.impl.AbstractConfigValue right = origRight;

        // check for an object which can be converted to a list
        // (this will be an object with numeric keys, like foo.0, foo.1)
        if (left instanceof com.gu.typesafe.config.ConfigObject && right instanceof SimpleConfigList) {
            left = DefaultTransformer.transform(left, com.gu.typesafe.config.ConfigValueType.LIST);
        } else if (left instanceof SimpleConfigList && right instanceof com.gu.typesafe.config.ConfigObject) {
            right = DefaultTransformer.transform(right, ConfigValueType.LIST);
        }

        // Since this depends on the type of two instances, I couldn't think
        // of much alternative to an instanceof chain. Visitors are sometimes
        // used for multiple dispatch but seems like overkill.
        com.gu.typesafe.config.impl.AbstractConfigValue joined = null;
        if (left instanceof com.gu.typesafe.config.ConfigObject && right instanceof com.gu.typesafe.config.ConfigObject) {
            joined = right.withFallback(left);
        } else if (left instanceof SimpleConfigList && right instanceof SimpleConfigList) {
            joined = ((SimpleConfigList)left).concatenate((SimpleConfigList)right);
        } else if ((left instanceof SimpleConfigList || left instanceof ConfigObject) &&
                   isIgnoredWhitespace(right)) {
            joined = left;
            // it should be impossible that left is whitespace and right is a list or object
        } else if (left instanceof ConfigConcatenation || right instanceof ConfigConcatenation) {
            throw new com.gu.typesafe.config.ConfigException.BugOrBroken("unflattened ConfigConcatenation");
        } else if (left instanceof com.gu.typesafe.config.impl.Unmergeable || right instanceof com.gu.typesafe.config.impl.Unmergeable) {
            // leave joined=null, cannot join
        } else {
            // handle primitive type or primitive type mixed with object or list
            String s1 = left.transformToString();
            String s2 = right.transformToString();
            if (s1 == null || s2 == null) {
                throw new com.gu.typesafe.config.ConfigException.WrongType(left.origin(),
                        "Cannot concatenate object or list with a non-object-or-list, " + left
                                + " and " + right + " are not compatible");
            } else {
                com.gu.typesafe.config.ConfigOrigin joinedOrigin = SimpleConfigOrigin.mergeOrigins(left.origin(),
                        right.origin());
                joined = new ConfigString.Quoted(joinedOrigin, s1 + s2);
            }
        }

        if (joined == null) {
            builder.add(right);
        } else {
            builder.remove(builder.size() - 1);
            builder.add(joined);
        }
    }