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

io.smallrye.config.ConfigValueMapStringView Maven / Gradle / Ivy

package io.smallrye.config;

import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

final class ConfigValueMapStringView extends AbstractMap {
    private final Map delegate;
    private final String configSourceName;
    private final int configSourceOrdinal;

    public ConfigValueMapStringView(final Map delegate, final String configSourceName,
            final int configSourceOrdinal) {
        this.delegate = Collections.unmodifiableMap(delegate);
        this.configSourceName = configSourceName;
        this.configSourceOrdinal = configSourceOrdinal;
    }

    @Override
    public int size() {
        return delegate.size();
    }

    @Override
    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    @Override
    public boolean containsKey(final Object key) {
        return delegate.containsKey(key);
    }

    @Override
    public boolean containsValue(final Object value) {
        ConfigValue configValue = (ConfigValue) value;
        if (configValue == null || configValue.getValue() == null) {
            return delegate.containsValue(null);
        }
        return delegate.containsValue(configValue.getValue());
    }

    @Override
    public ConfigValue get(final Object key) {
        final String value = delegate.get(key);
        if (value == null) {
            return null;
        }

        return toConfigValue((String) key, value);
    }

    @Override
    public Set keySet() {
        return delegate.keySet();
    }

    private Set> entrySet;
    private Collection values;

    @Override
    public Set> entrySet() {
        if (entrySet == null) {
            entrySet = new AbstractSet>() {
                @Override
                public Iterator> iterator() {
                    return new Iterator>() {
                        final Iterator> delegate = ConfigValueMapStringView.this.delegate.entrySet()
                                .iterator();

                        @Override
                        public boolean hasNext() {
                            return delegate.hasNext();
                        }

                        @Override
                        public Entry next() {
                            final Entry next = delegate.next();
                            final String value = next.getValue();
                            return value != null
                                    ? new SimpleImmutableEntry<>(next.getKey(), toConfigValue(next.getKey(), value))
                                    : new SimpleImmutableEntry<>(next.getKey(), null);
                        }
                    };
                }

                @Override
                public int size() {
                    return delegate.size();
                }
            };
        }
        return entrySet;
    }

    @Override
    public Collection values() {
        if (values == null) {
            values = new AbstractCollection() {
                @Override
                public Iterator iterator() {
                    final Iterator> delegate = ConfigValueMapStringView.this.entrySet().iterator();

                    return new Iterator() {
                        @Override
                        public boolean hasNext() {
                            return delegate.hasNext();
                        }

                        @Override
                        public ConfigValue next() {
                            final Entry next = delegate.next();
                            return next != null ? next.getValue() : null;
                        }
                    };
                }

                @Override
                public int size() {
                    return delegate.size();
                }
            };
        }
        return values;

    }

    private ConfigValue toConfigValue(final String name, final String value) {
        return ConfigValue.builder()
                .withName(name)
                .withValue(value)
                .withRawValue(value)
                .withConfigSourceName(configSourceName)
                .withConfigSourceOrdinal(configSourceOrdinal)
                .build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy