All Downloads are FREE. Search and download functionalities are using the official Maven repository.

alexh.weak.DynamicMap Maven / Gradle / Ivy

There is a newer version: 4.0
Show newest version
/*
 * Copyright 2015 Alex Butler
 *
 * 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 alexh.weak;

import static alexh.weak.DynamicChildLogic.using;
import alexh.LiteJoiner;
import java.util.Map;
import java.util.stream.Stream;

class DynamicMap extends AbstractDynamic> implements Dynamic, Describer {

    public DynamicMap(Map inner) {
        super(inner);
    }

    @Override
    public Dynamic get(Object childKey) {
        if (inner.isEmpty()) return new ParentAbsence.Empty<>(this, childKey);
        if (!inner.containsKey(childKey)) {
            if (childKey instanceof String) {
                for (Map.Entry entry : inner.entrySet()) {
                    if (childKey.equals(entry.getKey().toString()))
                        return entry.getValue() != null ? DynamicChild.from(this, childKey, entry.getValue()) :
                            new ChildAbsence.Null(this, childKey);
                }
            }

            final String keyString = childKey.toString();
            if (inner.containsKey(keyString)) {
                final Object val = inner.get(keyString);
                return val != null ? DynamicChild.from(this, keyString, val) : new ChildAbsence.Null(this, keyString);
            }

            return new ChildAbsence.Missing<>(this, childKey);
        }
        final Object val = inner.get(childKey);
        return val != null ? DynamicChild.from(this, childKey, val) : new ChildAbsence.Null(this, childKey);
    }

    @Override
    public Stream children() {
        return inner.keySet().stream().map(this::get);
    }

    @Override
    public String describe() {
        if (inner.isEmpty()) return "Empty-Map";
        return "Map" + inner.keySet().toString();
    }

    @Override
    public Object keyLiteral() {
        return ROOT_KEY;
    }

    @Override
    public String toString() {
        return keyLiteral() + ":"+ describe();
    }

    static class Child extends DynamicMap implements DynamicChild {

        private final Dynamic parent;
        private final Object key;

        Child(Dynamic parent, Object key, Map inner) {
            super(inner);
            this.parent = parent;
            this.key = key;
        }

        @Override
        public Dynamic parent() {
            return parent;
        }

        @Override
        public Object keyLiteral() {
            return key;
        }

        @Override
        public String toString() {
            return LiteJoiner.on(ARROW).join(using(this).getAscendingKeyChainWithRoot()) + ":" + describe();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy