001/* 002 * Copyright 2024 Anyware Services 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 */ 016package org.ametys.runtime.parameter; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.List; 021 022import org.ametys.runtime.i18n.I18nizableText; 023 024/** 025 * Result structure to populate on validation. The result can contain information, warnings and errors 026 * @see Validator 027 */ 028public class ValidationResult 029{ 030 private List<I18nizableText> _infos = new ArrayList<>(); 031 private List<I18nizableText> _warnings = new ArrayList<>(); 032 private List<I18nizableText> _errors = new ArrayList<>(); 033 034 /** 035 * Tests if there were any information. 036 * @return <code>true</code> if there is at least one information, 037 * <code>false</code> if there is no information. 038 */ 039 public boolean hasInfos() 040 { 041 return !_infos.isEmpty(); 042 } 043 044 /** 045 * Retrieves the information. 046 * @return the information. 047 */ 048 public List<I18nizableText> getInfos() 049 { 050 return Collections.unmodifiableList(_infos); 051 } 052 053 /** 054 * Add an information. 055 * @param infoLabel the information label. 056 */ 057 public void addInfo(I18nizableText infoLabel) 058 { 059 _infos.add(infoLabel); 060 } 061 062 /** 063 * Add several information. 064 * @param infoLabels the information labels. 065 */ 066 public void addInfos(List<I18nizableText> infoLabels) 067 { 068 _infos.addAll(infoLabels); 069 } 070 071 /** 072 * Tests if there were any warnings. 073 * @return <code>true</code> if there is at least one warning, 074 * <code>false</code> if there is no warning. 075 */ 076 public boolean hasWarnings() 077 { 078 return !_warnings.isEmpty(); 079 } 080 081 /** 082 * Retrieves the warnings. 083 * @return the warnings. 084 */ 085 public List<I18nizableText> getWarnings() 086 { 087 return Collections.unmodifiableList(_warnings); 088 } 089 090 /** 091 * Add a warning. 092 * @param warningLabel the warning label. 093 */ 094 public void addWarning(I18nizableText warningLabel) 095 { 096 _warnings.add(warningLabel); 097 } 098 099 /** 100 * Add warnings. 101 * @param warningLabels the warning labels. 102 */ 103 public void addWarnings(List<I18nizableText> warningLabels) 104 { 105 _warnings.addAll(warningLabels); 106 } 107 108 /** 109 * Tests if there were any errors. 110 * @return <code>true</code> if there is at least one error, 111 * <code>false</code> if there is no error. 112 */ 113 public boolean hasErrors() 114 { 115 return !_errors.isEmpty(); 116 } 117 118 /** 119 * Retrieves the errors. 120 * @return the errors. 121 */ 122 public List<I18nizableText> getErrors() 123 { 124 return Collections.unmodifiableList(_errors); 125 } 126 127 /** 128 * Add an error. 129 * @param errorLabel the error label. 130 */ 131 public void addError(I18nizableText errorLabel) 132 { 133 _errors.add(errorLabel); 134 } 135 136 /** 137 * Add errors. 138 * @param errorLabels the error labels. 139 */ 140 public void addErrors(List<I18nizableText> errorLabels) 141 { 142 _errors.addAll(errorLabels); 143 } 144 145 /** 146 * Add items of the given result to the current one 147 * @param result the result to add 148 */ 149 public void addResult(ValidationResult result) 150 { 151 addInfos(result.getInfos()); 152 addWarnings(result.getWarnings()); 153 addErrors(result.getErrors()); 154 } 155 156 /** 157 * Check if the current result is empty 158 * @return <code>true</code> if the result is empty, <code>false</code> otherwise 159 */ 160 public boolean isEmpty() 161 { 162 return !hasErrors() && !hasWarnings() && !hasInfos(); 163 } 164 165 /** 166 * Retrieves an empty instance of {@link ValidationResult} 167 * @return an empty {@link ValidationResult} 168 */ 169 public static ValidationResult empty() 170 { 171 return new ValidationResult(); 172 } 173}