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