001/* 002 * This file is part of the Kompics component model runtime. 003 * 004 * Copyright (C) 2009 Swedish Institute of Computer Science (SICS) 005 * Copyright (C) 2009 Royal Institute of Technology (KTH) 006 * 007 * This program is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU General Public License 009 * as published by the Free Software Foundation; either version 2 010 * of the License, or (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with this program; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 020 */ 021package se.sics.kompics.config; 022 023import java.util.HashMap; 024import java.util.Iterator; 025import java.util.Map.Entry; 026import java.util.UUID; 027 028/** 029 * 030 * @author Lars Kroll {@literal <[email protected]>} 031 */ 032public class ConfigUpdate implements Iterable<Entry<String, ConfigValue>> { 033 034 final HashMap<String, ConfigValue> updates; 035 final long versionId; 036 final UUID creator; 037 038 ConfigUpdate(HashMap<String, ConfigValue> updates, long versionId, UUID creator) { 039 this.updates = updates; 040 this.versionId = versionId; 041 this.creator = creator; 042 } 043 044 @Override 045 public Iterator<Entry<String, ConfigValue>> iterator() { 046 return updates.entrySet().iterator(); 047 } 048 049 public ConfigUpdateFactory modify(UUID creator) { 050 return new Factory(creator); 051 } 052 053 public class Factory implements ConfigUpdateFactory { 054 055 private final HashMap<String, ConfigValue> updates = new HashMap<>(); 056 private final UUID creator; 057 058 private Factory(UUID creator) { 059 this.creator = creator; 060 } 061 062 @Override 063 public void include(String key, ConfigValue original) { 064 updates.put(key, original); 065 } 066 067 @Override 068 public void replace(String key, ConfigValue original, Object replacement) { 069 ConfigValue newCV = Config.Builder.CVFactory.INSTANCE.create(replacement, original.version(), 070 original.options()); 071 updates.put(key, newCV); 072 } 073 074 @Override 075 public void replace(String key, ConfigValue original, Object replacement, ValueOptions options) { 076 ConfigValue newCV = Config.Builder.CVFactory.INSTANCE.create(replacement, original.version(), options); 077 updates.put(key, newCV); 078 } 079 080 @Override 081 public ConfigUpdate assemble() { 082 return new ConfigUpdate(this.updates, ConfigUpdate.this.versionId, this.creator); 083 } 084 } 085}