/* * Copyright 2000-2016 JetBrains s.r.o. * * 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 jetbrains.buildServer.agent.rakerunner.utils; import java.util.HashMap; import org.jetbrains.annotations.NotNull; import java.util.*; /** * @author Vladislav.Rassokhin */ public class EnvironmentPatchableMap implements Map { public EnvironmentPatchableMap(final Map baseEnv) { myBaseEnvironment = Collections.unmodifiableMap(new HashMap(baseEnv)); myPatchedEnvironment = new HashMap(); myPatchedEnvironment.putAll(baseEnv); //myOperationsList = new ArrayList(); } /** * @return read-only map */ public Map getBase() { return myBaseEnvironment; } /** * @return modifiable map */ public Map getPatched() { return this; } /** * @return set of removed keys (exist in base and not exist in pathced) */ @NotNull public Set getRemovedKeys() { final Set removed = new HashSet(Math.max(0, getBase().size() - size())); for (String key : getBase().keySet()) { if (!containsKey(key)) { removed.add(key); } } return removed; } //private class Operation { // OperationType type; // String key; // String value; // Null if type == Unset //} // //private enum OperationType { // Set, // Unset; //} // TODO: add calculate patch method private final Map myBaseEnvironment; private final Map myPatchedEnvironment; //private final List myOperationsList; public int size() { return myPatchedEnvironment.size(); } public boolean isEmpty() { return myPatchedEnvironment.isEmpty(); } public boolean containsKey(final Object key) { return myPatchedEnvironment.containsKey(key); } public boolean containsValue(final Object value) { return myPatchedEnvironment.containsValue(value); } public String get(final Object key) { return myPatchedEnvironment.get(key); } public String put(final String key, final String value) { return myPatchedEnvironment.put(key, value); } public String remove(final Object key) { return myPatchedEnvironment.remove(key); } public void putAll(final Map m) { myPatchedEnvironment.putAll(m); } public void clear() { myPatchedEnvironment.clear(); } public Set keySet() { return myPatchedEnvironment.keySet(); } public Collection values() { return myPatchedEnvironment.values(); } public Set> entrySet() { return myPatchedEnvironment.entrySet(); } }