/* Copyright 2010 Abhinav Sarkar * * This file is a part of SpelHelper library. * * SpelHelper library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License (GNU LGPL) as * published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * SpelHelper library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with SpelHelper library. If not, see . */ package net.abhinavsarkar.spelhelper; import static java.util.Collections.unmodifiableList; import static java.util.Collections.unmodifiableSet; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; /** * Provides some implicit methods which can be invoked on the instances of * class of the first parameter of the method inside a SpEL expression. * @author Abhinav Sarkar _abhinav@abhinavsarkar.net_ */ public final class ImplicitMethods { /** * Provides implicit method `distinct` on the {@link List} class. * * Example: `"#list('a','b','a').distinct()" //should return List('a','b')` * * With implicit property support provided by {@link SpelHelper} this can * also be written as: * * `"#list('a','b','a').distinct" //same output as earlier` * @param Type of the list's elements. * @param list The list to call this method upon. * @return An unmodifiable {@link Set} containing the distinct items of the list. */ public static Set distinct(final List list) { return unmodifiableSet(new HashSet(list)); } /** * Provides implicit method `sorted` on the {@link List} class. * * Example: `"#list('c','b','a').sorted()" //should return List('a','b','c')` * * With implicit property support provided by {@link SpelHelper} this can * also be written as: * * `"#list('c','b','a').sorted" //same output as earlier` * @param Type of the list's elements. * @param list The list to call this method upon. * @return An unmodifiable {@link List} containing the sorted items * of the list. * @see Collections#sort(List) */ public static > List sorted( final List list) { List temp = new ArrayList(list); Collections.sort(temp); return unmodifiableList(temp); } /** * Provides implicit method `reversed` on the {@link List} class. * * Example: `"#list('c','b','a').reversed()" //should return List('a','b','c')` * * With implicit property support provided by {@link SpelHelper} this can * also be written as: * * `"#list('c','b','a').reversed" //same output as earlier` * @param Type of the list's elements. * @param list The list to call this method upon. * @return An unmodifiable {@link List} containing the items of the * list in reverse order. * @see Collections#reverse(List) */ public static List reversed(final List list) { List temp = new ArrayList(list); Collections.reverse(temp); return unmodifiableList(temp); } /** * Provides implicit method `take` on the {@link List} class. * * Example: `"#list('c','b','a').take(2)" //should return List('a','b')` * * @param Type of the list's elements. * @param list The list to call this method upon. * @param n Number of items to _take_ from the list. * @return An unmodifiable {@link List} containing the first `n` items * of the list. */ public static List take(final List list, final int n) { return unmodifiableList(list.subList(0, n)); } /** * Provides implicit method `drop` on the {@link List} class. * * Example: `"#list('c','b','a').drop(2)" //should return List('a')` * * @param Type of the list's elements. * @param list The list to call this method upon. * @param n Number of items to _drop_ from the list. * @return An unmodifiable {@link List} containing the items after the * first `n` items of the list. */ public static List drop(final List list, final int n) { return unmodifiableList(list.subList(n, list.size())); } }