001/** 002 * Copyright (C) 2006-2024 Talend Inc. - www.talend.com 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.talend.sdk.component.runtime.manager.reflect.parameterenricher; 017 018import java.lang.annotation.Annotation; 019import java.lang.reflect.Type; 020import java.util.Arrays; 021import java.util.Collection; 022import java.util.Collections; 023import java.util.List; 024import java.util.Map; 025import java.util.function.Function; 026import java.util.stream.Collectors; 027import java.util.stream.Stream; 028 029import org.talend.sdk.component.api.configuration.constraint.Max; 030import org.talend.sdk.component.api.configuration.constraint.Min; 031 032/** 033 * @author Oleksand Zhelezniak 034 */ 035public class IntegerConstraintEnricher extends BaseParameterEnricher { 036 037 @Override 038 public Map<String, String> onParameterAnnotation(final String parameterName, final Type parameterType, 039 final Annotation annotation) { 040 return Collections.emptyMap(); 041 } 042 043 @Override 044 public Map<Type, Collection<Annotation>> getImplicitAnnotationForTypes() { 045 // we can re-use existed constraint enricher 046 // also we can differentiate that those constraints are implicit 047 // and they can be overwritten by explicit 048 final List<Annotation> annotations = Arrays.asList(new PseudoMin(), new PseudoMax()); 049 return Stream.of(int.class, Integer.class) 050 .collect(Collectors.toMap(Function.identity(), k -> annotations)); 051 } 052 053 private static final class PseudoMin implements Min { 054 055 @Override 056 public double value() { 057 return Integer.MIN_VALUE; 058 } 059 060 @Override 061 public Class<? extends Annotation> annotationType() { 062 return Min.class; 063 } 064 } 065 066 private static final class PseudoMax implements Max { 067 068 @Override 069 public double value() { 070 return Integer.MAX_VALUE; 071 } 072 073 @Override 074 public Class<? extends Annotation> annotationType() { 075 return Max.class; 076 } 077 } 078}