001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.config.mod;
018
019import java.util.function.*;
020
021/**
022 * Specifies an entry modifier that is used to encode during write and decode during read of config entries.
023 */
024public class Mod {
025
026   //-----------------------------------------------------------------------------------------------------------------
027   // Static
028   //-----------------------------------------------------------------------------------------------------------------
029
030   /** A no-op modifier. */
031   public static final Mod NO_OP = new Mod(' ', x -> x, x -> x, x -> true);
032
033   //-----------------------------------------------------------------------------------------------------------------
034   // Instance
035   //-----------------------------------------------------------------------------------------------------------------
036
037   private final char id;
038   private final Function<String,String> removeFunction, applyFunction;
039   private final Function<String,Boolean> detectFunction;
040
041   /**
042    * Constructor.
043    *
044    * @param id The character identifier.
045    * @param applyFunction
046    *    The function to apply when writing an entry.
047    *    Can be <jk>null</jk> if you override the {@link #apply(String)} method.
048    * @param removeFunction
049    *    The function to apply when reading an entry.
050    *    Can be <jk>null</jk> if you override the {@link #remove(String)} method.
051    * @param detectFunction
052    *    The function to apply to detect whether the modification has been made.
053    *    Can be <jk>null</jk> if you override the {@link #isApplied(String)} method.
054    */
055   public Mod(char id, Function<String,String> applyFunction, Function<String,String> removeFunction, Function<String,Boolean> detectFunction) {  // NOSONAR - Intentional.
056      this.id = id;
057      this.applyFunction = applyFunction;
058      this.removeFunction = removeFunction;
059      this.detectFunction = detectFunction;
060   }
061
062   /**
063    * Returns the modifier identifier character.
064    *
065    * @return The modifier identifier character.
066    */
067   public char getId() {
068      return id;
069   }
070
071   /**
072    * Detects whether this modification has been applied.
073    *
074    * @param value The entry value being tested.  Will never be <jk>null</jk>.
075    * @return <jk>true</jk> if the modification has been made to the entry.
076    */
077   public boolean isApplied(String value) {
078      return detectFunction.apply(value);
079   }
080
081   /**
082    * Applies this modification to the specified entry value.
083    *
084    * <p>
085    * Will only be called if {@link #isApplied(String)} returns <jk>false</jk>.
086    *
087    * @param value The entry value being written.  Will never be <jk>null</jk>.
088    * @return The modified value.
089    */
090   public String apply(String value) {
091      return applyFunction.apply(value);
092   }
093
094   /**
095    * Removes this modification to the specified entry value.
096    *
097    * <p>
098    * Will only be called if {@link #isApplied(String)} returns <jk>true</jk>.
099    *
100    * @param value The entry value being read.  Will never be <jk>null</jk>.
101    * @return The unmodified value.
102    */
103   public String remove(String value) {
104      return removeFunction.apply(value);
105   }
106
107   /**
108    * Applies this modification to the specified entry value if it isn't already applied.
109    *
110    * @param value The entry value being written.  Will never be <jk>null</jk>.
111    * @return The modified value.
112    */
113   public final String doApply(String value) {
114      return isApplied(value) ? value : apply(value);
115   }
116
117   /**
118    * Removes this modification from the specified entry value if it is applied.
119    *
120    * @param value The entry value being written.  Will never be <jk>null</jk>.
121    * @return The modified value.
122    */
123   public final String doRemove(String value) {
124      return isApplied(value) ? remove(value) : value;
125   }
126}