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   /** A no-op modifier. */
026   public static final Mod NO_OP = new Mod(' ', x -> x, x -> x, x -> true);
027   private final char id;
028   private final Function<String,String> removeFunction, applyFunction;
029   private final Function<String,Boolean> detectFunction;
030
031   /**
032    * Constructor.
033    *
034    * @param id The character identifier.
035    * @param applyFunction
036    *    The function to apply when writing an entry.
037    *    Can be <jk>null</jk> if you override the {@link #apply(String)} method.
038    * @param removeFunction
039    *    The function to apply when reading an entry.
040    *    Can be <jk>null</jk> if you override the {@link #remove(String)} method.
041    * @param detectFunction
042    *    The function to apply to detect whether the modification has been made.
043    *    Can be <jk>null</jk> if you override the {@link #isApplied(String)} method.
044    */
045   public Mod(char id, Function<String,String> applyFunction, Function<String,String> removeFunction, Function<String,Boolean> detectFunction) {  // NOSONAR - Intentional.
046      this.id = id;
047      this.applyFunction = applyFunction;
048      this.removeFunction = removeFunction;
049      this.detectFunction = detectFunction;
050   }
051
052   /**
053    * Applies this modification to the specified entry value.
054    *
055    * <p>
056    * Will only be called if {@link #isApplied(String)} returns <jk>false</jk>.
057    *
058    * @param value The entry value being written.  Will never be <jk>null</jk>.
059    * @return The modified value.
060    */
061   public String apply(String value) {
062      return applyFunction.apply(value);
063   }
064
065   /**
066    * Applies this modification to the specified entry value if it isn't already applied.
067    *
068    * @param value The entry value being written.  Will never be <jk>null</jk>.
069    * @return The modified value.
070    */
071   public final String doApply(String value) {
072      return isApplied(value) ? value : apply(value);
073   }
074
075   /**
076    * Removes this modification from the specified entry value if it is applied.
077    *
078    * @param value The entry value being written.  Will never be <jk>null</jk>.
079    * @return The modified value.
080    */
081   public final String doRemove(String value) {
082      return isApplied(value) ? remove(value) : value;
083   }
084
085   /**
086    * Returns the modifier identifier character.
087    *
088    * @return The modifier identifier character.
089    */
090   public char getId() { return id; }
091
092   /**
093    * Detects whether this modification has been applied.
094    *
095    * @param value The entry value being tested.  Will never be <jk>null</jk>.
096    * @return <jk>true</jk> if the modification has been made to the entry.
097    */
098   public boolean isApplied(String value) {
099      return detectFunction.apply(value);
100   }
101
102   /**
103    * Removes this modification to the specified entry value.
104    *
105    * <p>
106    * Will only be called if {@link #isApplied(String)} returns <jk>true</jk>.
107    *
108    * @param value The entry value being read.  Will never be <jk>null</jk>.
109    * @return The unmodified value.
110    */
111   public String remove(String value) {
112      return removeFunction.apply(value);
113   }
114}