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