001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.server.validation;
018
019import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
020import com.echothree.util.common.message.ExecutionErrors;
021import com.echothree.util.server.control.BaseLogic;
022import com.echothree.util.server.message.ExecutionErrorAccumulator;
023import static java.lang.Math.toIntExact;
024import java.util.Arrays;
025import java.util.Objects;
026
027public class ParameterUtils
028        extends BaseLogic {
029
030    private ParameterUtils() {
031        super();
032    }
033
034    private static class ParameterUtilsHolder {
035        static ParameterUtils instance = new ParameterUtils();
036    }
037
038    public static ParameterUtils getInstance() {
039        return ParameterUtils.ParameterUtilsHolder.instance;
040    }
041
042    public int countNonNullParameters(Object... objects) {
043        return toIntExact(Arrays.stream(objects)
044                .filter(Objects::nonNull)
045                .count());
046    }
047
048    public int countNullParameters(Object... objects) {
049        return toIntExact(Arrays.stream(objects)
050                .filter(Objects::isNull)
051                .count());
052    }
053
054    public boolean isExactlyOneBooleanTrue(Boolean... booleans) {
055        var areAnyTrue = false;
056        var areTwoTrue = false;
057
058        for(var i = 0; !areTwoTrue && (i < booleans.length); i++) {
059            areTwoTrue = (areAnyTrue && booleans[i]);
060            areAnyTrue |= booleans[i];
061        }
062
063        return areAnyTrue && !areTwoTrue;
064    }
065
066    public boolean isExactlyOneBooleanTrue(final ExecutionErrorAccumulator eea, Boolean... booleans) {
067        var result = isExactlyOneBooleanTrue(booleans);
068        
069        if(!result) {
070            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
071        }
072
073        return result;
074    }
075
076    public boolean isExactlyOneParameterPresent(String... parameters) {
077        var nullTestedParameters = Arrays.stream(parameters)
078                .map(Objects::nonNull)
079                .toArray(Boolean[]::new);
080
081        return isExactlyOneBooleanTrue(nullTestedParameters);
082    }
083
084    public boolean isExactlyOneParameterPresent(final ExecutionErrorAccumulator eea, String... parameters) {
085        var result = isExactlyOneParameterPresent(parameters);
086
087        if(!result) {
088            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
089        }
090
091        return result;
092    }
093
094}