com.iofairy.pattern.PatternIn Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functional Show documentation
Show all versions of functional Show documentation
Functional Programming for Java 8+ and compatible with the modular system of Java 9+.
/*
* Copyright (C) 2021 iofairy,
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.iofairy.pattern;
import java.util.Arrays;
import java.util.List;
/**
* PatternIn for matching multi-values in one time.
*
* @since 0.0.1
*/
public class PatternIn {
private List vs;
/**
* Match multi-values in one time.
* 判断 待匹配的值是否在集合中,也可用于一次匹配多个值:
* Examples:
* {@code
* .when(in(0, 1, 2), v -> {System.out.println("match!");})
*
* // it is equivalent to the code below
* int i = 1;
* switch (i) {
* case 0:
* case 1:
* case 2:
* System.out.println("match!");
* break;
* default:
* ...
* }
* }
*
* @param values multi-values
* @param values type
* @return PatternIn
* @since 0.0.1
*/
@SafeVarargs
public static PatternIn in(T... values) {
PatternIn patternIn = new PatternIn<>();
if (values == null) {
patternIn.vs = Arrays.asList((T) null);
return patternIn;
}
if (values.length == 0) throw new RuntimeException("The params's length must be greater than 0. 参数个数必须大于0");
patternIn.vs = Arrays.asList(values);
return patternIn;
}
public List getVs() {
return vs;
}
}