001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.base.string;
017
018import static org.kuali.common.util.base.Precondition.checkNotNull;
019
020import java.util.Map;
021
022import com.google.common.collect.BiMap;
023import com.google.common.collect.HashBiMap;
024import com.google.common.collect.ImmutableBiMap;
025
026public final class Replacer {
027
028        private final ImmutableBiMap<String, String> tokens;
029
030        public String replace(final String string) {
031                String s = string;
032                for (Map.Entry<String, String> pair : tokens.entrySet()) {
033                        s = s.replace(pair.getKey(), pair.getValue());
034                }
035                return s;
036        }
037
038        public String restore(final String string) {
039                String s = string;
040                for (Map.Entry<String, String> pair : tokens.entrySet()) {
041                        s = s.replace(pair.getValue(), pair.getKey());
042                }
043                return s;
044        }
045
046        private Replacer(Builder builder) {
047                this.tokens = ImmutableBiMap.copyOf(builder.tokens);
048        }
049
050        public static Replacer create(String oldToken, String newToken) {
051                return builder().add(oldToken, newToken).build();
052        }
053
054        public static Builder builder() {
055                return new Builder();
056        }
057
058        public static class Builder implements org.apache.commons.lang3.builder.Builder<Replacer> {
059
060                private BiMap<String, String> tokens = HashBiMap.create();
061
062                public Builder add(String oldToken, String newToken) {
063                        this.tokens.put(oldToken, newToken);
064                        return this;
065                }
066
067                public Builder tokens(BiMap<String, String> tokens) {
068                        this.tokens = tokens;
069                        return this;
070                }
071
072                @Override
073                public Replacer build() {
074                        Replacer instance = new Replacer(this);
075                        validate(instance);
076                        return instance;
077                }
078
079                private static void validate(Replacer instance) {
080                        checkNotNull(instance.tokens, "tokens");
081                }
082
083                public BiMap<String, String> getTokens() {
084                        return tokens;
085                }
086
087                public void setTokens(BiMap<String, String> tokens) {
088                        this.tokens = tokens;
089                }
090        }
091
092        public BiMap<String, String> getTokens() {
093                return tokens;
094        }
095
096}