001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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; 026import javax.enterprise.context.ApplicationScoped; 027 028@ApplicationScoped 029public class ParameterUtils 030 extends BaseLogic { 031 032 protected ParameterUtils() { 033 super(); 034 } 035 036 private static class ParameterUtilsHolder { 037 static ParameterUtils instance = new ParameterUtils(); 038 } 039 040 public static ParameterUtils getInstance() { 041 return ParameterUtils.ParameterUtilsHolder.instance; 042 } 043 044 public int countNonNullParameters(Object... objects) { 045 return toIntExact(Arrays.stream(objects) 046 .filter(Objects::nonNull) 047 .count()); 048 } 049 050 public int countNullParameters(Object... objects) { 051 return toIntExact(Arrays.stream(objects) 052 .filter(Objects::isNull) 053 .count()); 054 } 055 056 public boolean isExactlyOneBooleanTrue(Boolean... booleans) { 057 var areAnyTrue = false; 058 var areTwoTrue = false; 059 060 for(var i = 0; !areTwoTrue && (i < booleans.length); i++) { 061 areTwoTrue = (areAnyTrue && booleans[i]); 062 areAnyTrue |= booleans[i]; 063 } 064 065 return areAnyTrue && !areTwoTrue; 066 } 067 068 public boolean isExactlyOneBooleanTrue(final ExecutionErrorAccumulator eea, Boolean... booleans) { 069 var result = isExactlyOneBooleanTrue(booleans); 070 071 if(!result) { 072 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 073 } 074 075 return result; 076 } 077 078 public boolean isExactlyOneParameterPresent(String... parameters) { 079 var nullTestedParameters = Arrays.stream(parameters) 080 .map(Objects::nonNull) 081 .toArray(Boolean[]::new); 082 083 return isExactlyOneBooleanTrue(nullTestedParameters); 084 } 085 086 public boolean isExactlyOneParameterPresent(final ExecutionErrorAccumulator eea, String... parameters) { 087 var result = isExactlyOneParameterPresent(parameters); 088 089 if(!result) { 090 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 091 } 092 093 return result; 094 } 095 096}