Java Code Examples for org.apache.commons.lang3.Validate#noNullElements()
The following examples show how to use
org.apache.commons.lang3.Validate#noNullElements() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Color.java From Kettle with GNU General Public License v3.0 | 6 votes |
/** * Creates a new color with its RGB components changed as if it was dyed * with the colors passed in, replicating vanilla workbench dyeing * * @param colors The colors to dye with * @return A new color with the changed rgb components */ // TODO: Javadoc what this method does, not what it mimics. API != Implementation public Color mixColors(Color... colors) { Validate.noNullElements(colors, "Colors cannot be null"); int totalRed = this.getRed(); int totalGreen = this.getGreen(); int totalBlue = this.getBlue(); int totalMax = Math.max(Math.max(totalRed, totalGreen), totalBlue); for (Color color : colors) { totalRed += color.getRed(); totalGreen += color.getGreen(); totalBlue += color.getBlue(); totalMax += Math.max(Math.max(color.getRed(), color.getGreen()), color.getBlue()); } float averageRed = totalRed / (colors.length + 1); float averageGreen = totalGreen / (colors.length + 1); float averageBlue = totalBlue / (colors.length + 1); float averageMax = totalMax / (colors.length + 1); float maximumOfAverages = Math.max(Math.max(averageRed, averageGreen), averageBlue); float gainFactor = averageMax / maximumOfAverages; return Color.fromRGB((int) (averageRed * gainFactor), (int) (averageGreen * gainFactor), (int) (averageBlue * gainFactor)); }
Example 2
Source File: ToolMetaItem.java From GregTech with GNU Lesser General Public License v3.0 | 5 votes |
public MetaToolValueItem addOreDict(ToolDictNames... oreDictNames) { Validate.notNull(oreDictNames, "Cannot add null ToolDictName."); Validate.noNullElements(oreDictNames, "Cannot add null ToolDictName."); for (ToolDictNames oreDict : oreDictNames) { OreDictionary.registerOre(oreDict.name(), getStackForm()); } return this; }
Example 3
Source File: Schedule.java From pragmatic-microservices-lab with MIT License | 5 votes |
Schedule(List<CarrierMovement> carrierMovements) { Validate.notNull(carrierMovements); Validate.noNullElements(carrierMovements); Validate.notEmpty(carrierMovements); this.carrierMovements = carrierMovements; }
Example 4
Source File: CarrierMovement.java From pragmatic-microservices-lab with MIT License | 5 votes |
public CarrierMovement(Location departureLocation, Location arrivalLocation, Date departureTime, Date arrivalTime) { Validate.noNullElements(new Object[]{departureLocation, arrivalLocation, departureTime, arrivalTime}); this.departureTime = departureTime; this.arrivalTime = arrivalTime; this.departureLocation = departureLocation; this.arrivalLocation = arrivalLocation; }
Example 5
Source File: Instrumenter.java From coroutines with GNU Lesser General Public License v3.0 | 5 votes |
/** * Constructs a {@link Instrumenter} object from a filesystem classpath (folders and JARs). * @param classpath classpath JARs and folders to use for instrumentation (this is needed by ASM to generate stack map frames). * @throws IOException if classes in the classpath could not be loaded up * @throws NullPointerException if any argument is {@code null} or contains {@code null} */ public Instrumenter(List<File> classpath) throws IOException { Validate.notNull(classpath); Validate.noNullElements(classpath); classRepo = new CompositeClassInformationRepository( new ClassResourceClassInformationRepository(Instrumenter.class.getClassLoader()), // access to core JRE classes FileSystemClassInformationRepository.create(classpath) // access to user classes ); }
Example 6
Source File: SearchUtils.java From coroutines with GNU Lesser General Public License v3.0 | 5 votes |
/** * Find static methods within a class. * @param methodNodes method nodes to search through * @return list of methods * @throws NullPointerException if any argument is {@code null} or contains {@code null} */ public static List<MethodNode> findStaticMethods(Collection<MethodNode> methodNodes) { Validate.notNull(methodNodes); Validate.noNullElements(methodNodes); List<MethodNode> ret = new ArrayList<>(); for (MethodNode methodNode : methodNodes) { if ((methodNode.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC) { ret.add(methodNode); } } return ret; }
Example 7
Source File: CarrierMovement.java From pragmatic-microservices-lab with MIT License | 5 votes |
public CarrierMovement(Location departureLocation, Location arrivalLocation, Date departureTime, Date arrivalTime) { Validate.noNullElements(new Object[]{departureLocation, arrivalLocation, departureTime, arrivalTime}); this.departureTime = departureTime; this.arrivalTime = arrivalTime; this.departureLocation = departureLocation; this.arrivalLocation = arrivalLocation; }
Example 8
Source File: Schedule.java From pragmatic-microservices-lab with MIT License | 5 votes |
Schedule(List<CarrierMovement> carrierMovements) { Validate.notNull(carrierMovements); Validate.noNullElements(carrierMovements); Validate.notEmpty(carrierMovements); this.carrierMovements = carrierMovements; }
Example 9
Source File: Schedule.java From pragmatic-microservices-lab with MIT License | 5 votes |
Schedule(List<CarrierMovement> carrierMovements) { Validate.notNull(carrierMovements); Validate.noNullElements(carrierMovements); Validate.notEmpty(carrierMovements); this.carrierMovements = carrierMovements; }
Example 10
Source File: AO_TargetExt.java From svg2vector with Apache License 2.0 | 5 votes |
/** * Creates a long description with supported targets. * @param descr original long description * @param supportedTargets targets * @return long description with added list of supported targets * @throws NullPointerException if argument was null * @throw IllegalArgumentException if target had null elements */ protected static String buildLongDescr(String descr, SvgTargets[] supportedTargets){ Validate.notNull(supportedTargets); Validate.noNullElements(supportedTargets); StrBuilder ret = new StrBuilder(); ret.append(descr); ret.append(" Supported targets are: ").appendWithSeparators(supportedTargets, ", "); return ret.toString(); }
Example 11
Source File: CarrierMovement.java From CargoTracker-J12015 with MIT License | 5 votes |
public CarrierMovement(Location departureLocation, Location arrivalLocation, Date departureTime, Date arrivalTime) { Validate.noNullElements(new Object[]{departureLocation, arrivalLocation, departureTime, arrivalTime}); this.departureTime = departureTime; this.arrivalTime = arrivalTime; this.departureLocation = departureLocation; this.arrivalLocation = arrivalLocation; }
Example 12
Source File: NetworkParser.java From Signals with GNU General Public License v3.0 | 5 votes |
private void setMap(List<String> map){ Validate.noNullElements(map); if(map.isEmpty()) throw new IllegalArgumentException("Empty network is not allowed!"); int xSize = map.get(0).length(); for(int y = 0; y < map.size(); y++) { String yLine = map.get(y); if(yLine.length() != xSize) throw new IllegalArgumentException("Inconsistent map lengths! Violating row: " + y + ", expecting " + xSize + ", got " + yLine.length()); } this.map = map; }
Example 13
Source File: FileSystemClassInformationRepository.java From coroutines with GNU Lesser General Public License v3.0 | 5 votes |
/** * Add classes contained within a list of JAR files and folders. Note that if a duplicate class is encountered, the original is kept. * @param classpath list of JARs and folders to scan * @throws NullPointerException if any argument is {@code null} or contains {@code null} elements * @throws IOException if an IO error occurs */ public void addClasspath(List<File> classpath) throws IOException { Validate.notNull(classpath); Validate.noNullElements(classpath); for (File classpathElement : classpath) { if (classpathElement.isFile()) { addJar(classpathElement); } else if (classpathElement.isDirectory()) { addDirectory(classpathElement); } else { throw new IllegalStateException(); } } }
Example 14
Source File: GenericGenerators.java From coroutines with GNU Lesser General Public License v3.0 | 5 votes |
/** * Generates instructions for a switch table. This does not automatically generate jumps at the end of each default/case statement. It's * your responsibility to either add the relevant jumps, throws, or returns at each default/case statement, otherwise the code will * just fall through (which is likely not what you want). * @param indexInsnList instructions to calculate the index -- must leave an int on the stack * @param defaultInsnList instructions to execute on default statement -- must leave the stack unchanged * @param caseStartIdx the number which the case statements start at * @param caseInsnLists instructions to execute on each case statement -- must leave the stack unchanged * @return instructions for a table switch * @throws NullPointerException if any argument is {@code null} or contains {@code null} * @throws IllegalArgumentException if any numeric argument is {@code < 0}, or if {@code caseInsnLists} is empty */ public static InsnList tableSwitch(InsnList indexInsnList, InsnList defaultInsnList, int caseStartIdx, InsnList... caseInsnLists) { Validate.notNull(defaultInsnList); Validate.notNull(indexInsnList); Validate.isTrue(caseStartIdx >= 0); Validate.notNull(caseInsnLists); Validate.noNullElements(caseInsnLists); Validate.isTrue(caseInsnLists.length > 0); InsnList ret = new InsnList(); LabelNode defaultLabelNode = new LabelNode(); LabelNode[] caseLabelNodes = new LabelNode[caseInsnLists.length]; for (int i = 0; i < caseInsnLists.length; i++) { caseLabelNodes[i] = new LabelNode(); } ret.add(indexInsnList); ret.add(new TableSwitchInsnNode(caseStartIdx, caseStartIdx + caseInsnLists.length - 1, defaultLabelNode, caseLabelNodes)); for (int i = 0; i < caseInsnLists.length; i++) { LabelNode caseLabelNode = caseLabelNodes[i]; InsnList caseInsnList = caseInsnLists[i]; if (caseInsnList != null) { ret.add(caseLabelNode); ret.add(caseInsnList); } } if (defaultInsnList != null) { ret.add(defaultLabelNode); ret.add(defaultInsnList); } return ret; }
Example 15
Source File: AggregateUtil.java From feilong-core with Apache License 2.0 | 4 votes |
/** * 迭代<code>beanIterable</code>,提取符合 <code>includePredicate</code>的元素的指定 <code>propertyNames</code> 元素的值 ,累计总和. * * <h3>示例:</h3> * * <blockquote> * * <pre class="code"> * User user1 = new User(10L); * user1.setName("刘备"); * user1.setAge(50); * * User user2 = new User(20L); * user1.setName("关羽"); * user2.setAge(50); * * User user3 = new User(100L); * user3.setName("张飞"); * user3.setAge(100); * * List{@code <User>} list = toList(user1, user2, user3); * Map{@code <String, BigDecimal>} map = AggregateUtil.sum(list, ConvertUtil.toArray("id", "age"), new Predicate{@code <User>}(){ * * {@code @Override} * public boolean evaluate(User user){ * return !"张飞".equals(user.getName()); * } * }); * LOGGER.debug(JsonUtil.format(map)); * * </pre> * * <b>返回:</b> * * <pre class="code"> * { * "id": 30, * "age": 100 * } * </pre> * * </blockquote> * * @param <O> * the generic type * @param beanIterable * bean Iterable,诸如List{@code <User>},Set{@code <User>}等 * @param propertyNames * 泛型O对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见 * <a href="../bean/BeanUtil.html#propertyName">propertyName</a> * @param includePredicate * the include predicate * @return 如果 <code>beanIterable</code> 是null或者empty,返回 {@link Collections#emptyMap()}<br> * 如果通过反射某个元素值是null,则使用默认值0代替,再进行累加<br> * 如果 <code>includePredicate</code> 是null,那么迭代所有的元素<br> * 如果<code>beanIterable</code>没有符合 <code>includePredicate</code>的元素,返回 <code>new LinkedHashMap</code> * @throws NullPointerException * 如果<code>propertyNames</code> 是null * @throws IllegalArgumentException * 果<code>propertyNames</code> 有元素 是null <br> */ public static <O> Map<String, BigDecimal> sum(Iterable<O> beanIterable,String[] propertyNames,Predicate<O> includePredicate){ if (isNullOrEmpty(beanIterable)){ return emptyMap(); } Validate.noNullElements(propertyNames, "propertyNames can't be null/empty!"); Map<String, BigDecimal> sumMap = newLinkedHashMap(IterableUtils.size(beanIterable)); for (O obj : beanIterable){ if (null != includePredicate && !includePredicate.evaluate(obj)){ continue; } for (String propertyName : propertyNames){ //如果通过反射某个元素值是null,则使用默认值0 代替 BigDecimal addValue = NumberUtil.getAddValue( defaultIfNull(sumMap.get(propertyName), 0), defaultIfNull(PropertyUtil.<Number> getProperty(obj, propertyName), 0)); sumMap.put(propertyName, addValue); } } return sumMap; }
Example 16
Source File: DateUtil.java From feilong-core with Apache License 2.0 | 4 votes |
/** * 将时间字符串 <code>dateString</code> 使用<b>一个或者多个</b>不同的 <code>datePattern</code> 模式按照顺序转换成date类型. * * <h3>示例:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.toDate("2016-02-33", DatePattern.COMMON_DATE) = 2016-03-04 * DateUtil.toDate("2016-06-28T01:21:12-0800", "yyyy-MM-dd'T'HH:mm:ssZ") = 2016-06-28 17:21:12 * DateUtil.toDate("2016-06-28T01:21:12+0800", "yyyy-MM-dd'T'HH:mm:ssZ") = 2016-06-28 01:21:12 * </pre> * * </blockquote> * * <h3>注意:</h3> * <blockquote> * <ol> * <li>转换的时候,使用日历的<b>宽松模式</b>,参见 {@link java.text.DateFormat#setLenient(boolean)},即支持传入"2016-02-33",会转换成 2016-03-04</li> * <li>如果能解析所有的字符串,那么视为成功</li> * <li>如果没有任何的模式匹配,将会抛出异常</li> * <li>如果转换有异常,会将 {@link ParseException} 转成 {@link IllegalArgumentException} 返回,是 UnCheckedException异常 ,不需要强制catch处理</li> * </ol> * </blockquote> * * <h3>示例:</h3> * * <blockquote> * * 经常我们会看到小伙伴写出下面的代码: * * <pre class="code"> * SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); * Date publishTimeDate = null; * try{ * publishTimeDate = format.parse(publishTime); * }catch (ParseException e1){ * e1.printStackTrace(); * } * </pre> * * <p> * 可以看到直接使用 {@code SimpleDateFormat} 来写代码的话,代码行数较多,并且还需要自行处理 ParseException checkedException异常, 而且catch里面一般都是写的废话 * </p> * * <p> * 此时你可以一行代码搞定: * </p> * * <pre class="code"> * * Date publishTimeDate = DateUtil.toDate(publishTime, DatePattern.COMMON_DATE_AND_TIME_WITHOUT_SECOND); * </pre> * * </blockquote> * * @param dateString * 时间字符串 * @param datePatterns * 模式,时间字符串的模式{@link DatePattern} * @return 如果 <code>dateString</code> 是null,抛出 {@link NullPointerException}<br> * 如果 <code>dateString</code> 是blank,抛出 {@link IllegalArgumentException}<br> * 如果 <code>datePatterns</code> 是 null,抛出 {@link NullPointerException}<br> * 如果 <code>datePatterns</code> 是 empty,抛出 {@link IllegalArgumentException}<br> * 如果 <code>datePatterns</code> 有元素是 null,抛出 {@link IllegalArgumentException}<br> * @see org.apache.commons.lang3.time.DateUtils#parseDate(String, String...) * @see <a href="http://stackoverflow.com/questions/4216745/java-string-to-date-conversion/">java-string-to-date-conversion</a> * @see <a href="http://stackoverflow.com/questions/4216745/java-string-to-date-conversion/22180505#22180505">java-string-to-date- * conversion/22180505#22180505</a> * @see <a href="http://stackoverflow.com/questions/2735023/convert-string-to-java-util-date">convert-string-to-java-util-date</a> * @since 1.7.3 change param to datePatterns array */ public static Date toDate(String dateString,String...datePatterns){ Validate.notBlank(dateString, "dateString can't be blank!"); Validate.notEmpty(datePatterns, "datePatterns can't be null!"); Validate.noNullElements(datePatterns, "datePatterns can't has null datePattern"); //--------------------------------------------------------------- try{ return DateUtils.parseDate(dateString, datePatterns); }catch (ParseException e){ String pattern = "dateString:[{}],use patterns:[{}],parse to date exception,message:[{}]"; throw new IllegalArgumentException(Slf4jUtil.format(pattern, dateString, datePatterns, e.getMessage()), e); } }
Example 17
Source File: CraftInventory.java From Kettle with GNU General Public License v3.0 | 4 votes |
public HashMap<Integer, ItemStack> addItem(ItemStack... items) { Validate.noNullElements(items, "Item cannot be null"); HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>(); /* TODO: some optimization * - Create a 'firstPartial' with a 'fromIndex' * - Record the lastPartial per Material * - Cache firstEmpty result */ for (int i = 0; i < items.length; i++) { ItemStack item = items[i]; while (true) { // Do we already have a stack of it? int firstPartial = firstPartial(item); // Drat! no partial stack if (firstPartial == -1) { // Find a free spot! int firstFree = firstEmpty(); if (firstFree == -1) { // No space at all! leftover.put(i, item); break; } else { // More than a single stack! if (item.getAmount() > getMaxItemStack()) { CraftItemStack stack = CraftItemStack.asCraftCopy(item); stack.setAmount(getMaxItemStack()); setItem(firstFree, stack); item.setAmount(item.getAmount() - getMaxItemStack()); } else { // Just store it setItem(firstFree, item); break; } } } else { // So, apparently it might only partially fit, well lets do just that ItemStack partialItem = getItem(firstPartial); int amount = item.getAmount(); int partialAmount = partialItem.getAmount(); int maxAmount = partialItem.getMaxStackSize(); // Check if it fully fits if (amount + partialAmount <= maxAmount) { partialItem.setAmount(amount + partialAmount); // To make sure the packet is sent to the client setItem(firstPartial, partialItem); break; } // It fits partially partialItem.setAmount(maxAmount); // To make sure the packet is sent to the client setItem(firstPartial, partialItem); item.setAmount(amount + partialAmount - maxAmount); } } } return leftover; }
Example 18
Source File: OrePrefix.java From GregTech with GNU Lesser General Public License v3.0 | 4 votes |
public boolean addProcessingHandler(IOreRegistrationHandler... processingHandler) { Preconditions.checkNotNull(processingHandler); Validate.noNullElements(processingHandler); return oreProcessingHandlers.addAll(Arrays.asList(processingHandler)); }
Example 19
Source File: TestUtils.java From coroutines with GNU Lesser General Public License v3.0 | 3 votes |
/** * Writes entries to a JAR and loads it up. * @param entries class nodes to put in to jar * @return class loader with files in newly created JAR available * @throws IOException if any IO error occurs * @throws NullPointerException if any argument is {@code null} or contains {@code null} * @throws IllegalArgumentException if {@code classNodes} is empty */ public static URLClassLoader createJarAndLoad(JarEntry ... entries) throws IOException { Validate.notNull(entries); Validate.noNullElements(entries); File jarFile = createJar(entries); return URLClassLoader.newInstance(new URL[] { jarFile.toURI().toURL() }, TestUtils.class.getClassLoader()); }
Example 20
Source File: ConvertUtil.java From feilong-core with Apache License 2.0 | 3 votes |
/** * 将 <code>mapEntryCollection</code> 转成map ({@link LinkedHashMap}). * * <h3>说明:</h3> * * <blockquote> * <ol> * <li>返回是的是 {@link LinkedHashMap},顺序依照参数 <code>mapEntryCollection</code>,key是 {@link java.util.Map.Entry#getKey()},value 是 * {@link java.util.Map.Entry#getValue()}</li> * <li>{@link java.util.Map.Entry} 已知实现类,你可以使用 {@link Pair},或者 {@link java.util.AbstractMap.SimpleEntry}</li> * </ol> * </blockquote> * * <h3>{@link Pair} 示例:</h3> * * <blockquote> * * <pre class="code"> * Map{@code <String, String>} map = toMap(toList(// * Pair.of("张飞", "丈八蛇矛"), * Pair.of("关羽", "青龙偃月刀"), * Pair.of("赵云", "龙胆枪"), * Pair.of("刘备", "双股剑"))); * LOGGER.debug(JsonUtil.format(map)); * </pre> * * <b>返回:</b> * * <pre class="code"> * { * "张飞": "丈八蛇矛", * "关羽": "青龙偃月刀", * "赵云": "龙胆枪", * "刘备": "双股剑" * } * </pre> * * </blockquote> * * <h3>{@link java.util.AbstractMap.SimpleEntry} 示例:</h3> * * <blockquote> * * <pre class="code"> * Map{@code <String, String>} map = ConvertUtil.toMap( * toList( * new SimpleEntry{@code <>}("张飞", "丈八蛇矛"), * new SimpleEntry{@code <>}("关羽", "青龙偃月刀"), * new SimpleEntry{@code <>}("赵云", "龙胆枪"), * new SimpleEntry{@code <>}("刘备", "双股剑"))); * LOGGER.debug(JsonUtil.format(map)); * </pre> * * <b>返回:</b> * * <pre class="code"> * { * "张飞": "丈八蛇矛", * "关羽": "青龙偃月刀", * "赵云": "龙胆枪", * "刘备": "双股剑" * } * </pre> * * </blockquote> * * @param <V> * the value type * @param <K> * the key type * @param <E> * the element type * @param mapEntryCollection * the map entry collection * @return 如果 <code>mapEntryCollection</code> 是null,返回 {@link Collections#emptyMap()}<br> * 如果 <code>mapEntryCollection</code> 有元素是null,将会抛出异常 {@link IllegalArgumentException} * @see org.apache.commons.lang3.ArrayUtils#toMap(Object[]) * @since 1.7.1 */ public static <V, K, E extends Map.Entry<K, V>> Map<K, V> toMap(Collection<E> mapEntryCollection){ if (null == mapEntryCollection){ return emptyMap(); } Validate.noNullElements(mapEntryCollection, "mapEntryCollection can't has null elememt!"); Map<K, V> map = newLinkedHashMap(mapEntryCollection.size()); for (Map.Entry<K, V> entry : mapEntryCollection){ map.put(entry.getKey(), entry.getValue()); } return map; }