001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.util.events;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.jgrapes.core.Event;
026
027// TODO: Auto-generated Javadoc
028/**
029 * An event that triggers updates or deletions in a key/value store.
030 */
031public class KeyValueStoreUpdate extends Event<Void> {
032
033        private final List<Action> actions = new ArrayList<>();
034
035        /**
036         * Adds a new update action to the event.
037         * 
038         * @param key the key
039         * @param value the value
040         * @return the event for easy chaining
041         */
042        public KeyValueStoreUpdate update(String key, String value) {
043                actions.add(new Update(key, value));
044                return this;
045        }
046
047        /**
048         * Adds a new update action to the event that stores the given value
049         * on the path formed by the path segments.
050         * 
051         * @param value the value
052         * @param segments the path segments
053         * @return the event for easy chaining
054         */
055        public KeyValueStoreUpdate storeAs(String value, String... segments) {
056                actions.add(new Update("/" + String.join("/", segments), value));
057                return this;
058        }
059
060        /**
061         * Adds a new deletion action to the event.
062         * 
063         * @param key the key
064         * @return the event for easy chaining
065         */
066        public KeyValueStoreUpdate delete(String key) {
067                actions.add(new Deletion(key));
068                return this;
069        }
070        
071        /**
072         * Adds a new deletion action that clears all keys with the given
073         * path prefix.
074         * 
075         * @param segments the path segments
076         * @return the event for easy chaining
077         */
078        public KeyValueStoreUpdate clearAll(String... segments) {
079                actions.add(new Deletion("/" + String.join("/", segments)));
080                return this;
081        }
082
083        /**
084         * Returns the actions.
085         * 
086         * @return the actions
087         */
088        public List<Action> actions() {
089                return Collections.unmodifiableList(actions);
090        }
091        
092        /**
093         * The base class for all actions.
094         */
095        @SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
096        public abstract static class Action {
097                
098                private final String key;
099
100                /**
101                 * Instantiates a new action.
102                 *
103                 * @param key the key
104                 */
105                public Action(String key) {
106                        this.key = key;
107                }
108                
109                /**
110                 * Key.
111                 *
112                 * @return the string
113                 */
114                public String key() {
115                        return key;
116                }
117        }
118        
119        /**
120         * A key (and value) deletion.
121         */
122        public static class Deletion extends Action {
123                
124                /**
125                 * Instantiates a new deletion.
126                 *
127                 * @param key the key
128                 */
129                public Deletion(String key) {
130                        super(key);
131                }
132        }
133        
134        /**
135         * A value update.
136         */
137        public static class Update extends Action {
138                private final String value;
139
140                /**
141                 * Instantiates a new update.
142                 *
143                 * @param key the key
144                 * @param value the value
145                 */
146                public Update(String key, String value) {
147                        super(key);
148                        this.value = value;
149                }
150                
151                /**
152                 * Value.
153                 *
154                 * @return the string
155                 */
156                public String value() {
157                        return value;
158                }
159        }
160}