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.commons.settings;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020import static org.apache.juneau.commons.utils.ThrowableUtils.*;
021
022import java.util.function.*;
023
024import org.apache.juneau.commons.function.*;
025
026/**
027 * A writable {@link PropertyStore} implementation created from functional interfaces.
028 */
029@SuppressWarnings({
030   "java:S1192" // Duplicated literals (argument/property names) read more clearly inline than as constants
031})
032public class FunctionalPropertyStore implements PropertyStore {
033
034   private final UnaryOperator<String> reader;
035   private final BiConsumer<String,String> writer;
036   private final Consumer<String> unsetter;
037   private final Snippet clearer;
038
039   /**
040    * Constructor.
041    *
042    * @param reader The function used to read property values by name.  Must not be <jk>null</jk>.
043    * @param writer The function used to write property values.  Must not be <jk>null</jk>.
044    * @param unsetter The function used to remove a property by name.  Must not be <jk>null</jk>.
045    * @param clearer The function used to clear all properties.  Must not be <jk>null</jk>.
046    */
047   public FunctionalPropertyStore(
048      UnaryOperator<String> reader,
049      BiConsumer<String,String> writer,
050      Consumer<String> unsetter,
051      Snippet clearer
052   ) {
053      reqnn("reader", reader);
054      reqnn("writer", writer);
055      reqnn("unsetter", unsetter);
056      reqnn("clearer", clearer);
057      this.reader = reader;
058      this.writer = writer;
059      this.unsetter = unsetter;
060      this.clearer = clearer;
061   }
062
063   @Override
064   public PropertyLookupResult get(String name) {
065      var v = reader.apply(name);
066      return v == null ? PropertyLookupResult.missing() : PropertyLookupResult.present(o(v));
067   }
068
069   @Override
070   public void set(String name, String value) {
071      writer.accept(name, value);
072   }
073
074   @Override
075   public void unset(String name) {
076      unsetter.accept(name);
077   }
078
079   @Override
080   public void clear() {
081      safe(clearer::run);
082   }
083
084   /**
085    * Creates a new store from the specified functions.
086    *
087    * @param reader The function used to read property values by name.  Must not be <jk>null</jk>.
088    * @param writer The function used to write property values.  Must not be <jk>null</jk>.
089    * @param unsetter The function used to remove a property by name.  Must not be <jk>null</jk>.
090    * @param clearer The function used to clear all properties.  Must not be <jk>null</jk>.
091    * @return A new store.
092    */
093   public static FunctionalPropertyStore of(
094      UnaryOperator<String> reader,
095      BiConsumer<String,String> writer,
096      Consumer<String> unsetter,
097      Snippet clearer
098   ) {
099      return new FunctionalPropertyStore(reader, writer, unsetter, clearer);
100   }
101}