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; 022 023import java.util.Optional; 024import se.sics.kompics.config.ConfigUpdate; 025import se.sics.kompics.config.ConfigUpdateFactory; 026import se.sics.kompics.config.ValueMerger; 027 028/** 029 * 030 * @author Lars Kroll {@literal <[email protected]>} 031 */ 032public class UpdateAction { 033 034 public interface Mapper { 035 public ConfigUpdate map(ConfigUpdate original, ConfigUpdateFactory factory); 036 } 037 038 public enum Propagation { 039 040 SWALLOW, ORIGINAL, MAP; 041 } 042 043 public static final UpdateAction DEFAULT = UpdateAction.create().finalise(); 044 045 public final Propagation upStrategy; 046 public final Mapper upMapper; 047 public final Propagation selfStrategy; 048 public final Mapper selfMapper; 049 public final Propagation downStrategy; 050 public final Mapper downMapper; 051 public final Optional<ValueMerger> merger; 052 053 private UpdateAction(Propagation upStrategy, Mapper upMapper, Propagation selfStrategy, Mapper selfMapper, 054 Propagation downStrategy, Mapper downMapper, Optional<ValueMerger> merger) { 055 this.upStrategy = upStrategy; 056 this.upMapper = upMapper; 057 this.selfStrategy = selfStrategy; 058 this.selfMapper = selfMapper; 059 this.downStrategy = downStrategy; 060 this.downMapper = downMapper; 061 this.merger = merger; 062 } 063 064 public static Builder create() { 065 return new Builder(); 066 } 067 068 public static class Builder { 069 070 private Propagation upStrategy = Propagation.ORIGINAL; 071 private Mapper upMapper = null; 072 private Propagation selfStrategy = Propagation.ORIGINAL; 073 private Mapper selfMapper = null; 074 private Propagation downStrategy = Propagation.ORIGINAL; 075 private Mapper downMapper = null; 076 private Optional<ValueMerger> merger = Optional.empty(); 077 078 public UpdateAction finalise() { 079 return new UpdateAction(upStrategy, upMapper, selfStrategy, selfMapper, downStrategy, downMapper, merger); 080 } 081 082 public void originalUp() { 083 this.upStrategy = Propagation.ORIGINAL; 084 this.upMapper = null; 085 } 086 087 public void originalSelf() { 088 this.selfStrategy = Propagation.ORIGINAL; 089 this.selfMapper = null; 090 } 091 092 public void originalDown() { 093 this.downStrategy = Propagation.ORIGINAL; 094 this.downMapper = null; 095 } 096 097 public void swallowUp() { 098 this.upStrategy = Propagation.SWALLOW; 099 this.upMapper = null; 100 } 101 102 public void swallowSelf() { 103 this.selfStrategy = Propagation.SWALLOW; 104 this.selfMapper = null; 105 } 106 107 public void swallowDown() { 108 this.downStrategy = Propagation.SWALLOW; 109 this.downMapper = null; 110 } 111 112 public void mapUp(Mapper mapper) { 113 this.upStrategy = Propagation.MAP; 114 this.upMapper = mapper; 115 } 116 117 public void mapSelf(Mapper mapper) { 118 this.selfStrategy = Propagation.MAP; 119 this.selfMapper = mapper; 120 } 121 122 public void mapDown(Mapper mapper) { 123 this.downStrategy = Propagation.MAP; 124 this.downMapper = mapper; 125 } 126 127 public void customMergeWith(ValueMerger merger) { 128 this.merger = Optional.ofNullable(merger); 129 } 130 } 131}