/* * Eduardo, an IRC bot framework * Copyright (C) sk89q <http://www.sk89q.com> * Copyright (C) Eduardo team and contributors * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.sk89q.eduardo.util.config; import com.google.common.collect.MapMaker; import java.util.Collection; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; public final class ConfigObject extends AbstractConfigObject implements Map<Object, Object> { private final ConcurrentMap<Object, Object> map = new MapMaker().concurrencyLevel(1).makeMap(); public Config toConfig() { return new SimpleConfig(this); } @Override public int hashCode() { return map.hashCode(); } @Override public int size() { return map.size(); } @Override public boolean isEmpty() { return map.isEmpty(); } @Override public boolean containsKey(Object key) { return map.containsKey(key); } @Override public boolean containsValue(Object value) { return map.containsValue(value); } @Override public Object get(Object key) { return map.get(key); } @Override public Object put(Object key, Object value) { if (key == null || value == null) { return null; } return map.put(key, ConfigTypes.serialize(value)); } @Override public Object remove(Object key) { return map.remove(key); } @Override public void putAll(Map<?, ?> m) { map.putAll(m.entrySet().stream() .filter(e -> e.getKey() != null && e.getValue() != null) .collect(Collectors.toMap( p -> ConfigTypes.serialize(p.getKey()), p -> ConfigTypes.serialize(p.getValue())))); } @Override public void clear() { map.clear(); } @Override public Set<Object> keySet() { return map.keySet(); } @Override public Collection<Object> values() { return map.values(); } @Override public Set<Entry<Object, Object>> entrySet() { return map.entrySet(); } @Override public boolean equals(Object o) { return map.equals(o); } @Override public String toString() { return map.toString(); } }