Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.verhas.utils;
 
 import java.util.Set;

Some static function to be used in other classes to have some syntactic sugar. Some day I will move these to a separate package so that you can import it from maven repo to other projects. At the moment they are small and simple, copy them if you like.

Author(s):
Peter Verhas
public class Sugar {

    
Use this method to create a new style (java 1.5+) 'for' loop when some old library method returns iterator. For example the PGPSecretKeyRingCollection.getKeyRings() returns an Iterator. You can write the code
 for (final PGPSecretKeyRing kRing : in((Iterator<PGPSecretKeyRing>) pgpSec
 .getKeyRings())) {
 ...
 }
 
(actual code was copied from License.java).

Parameters:
<T> note that you can not pass a raw Iterator. You have to cast it to the specific generic type Iterator as in the example above.
iterator is 'converted'
Returns:
an Iterable that will iterate through the elements of the iterator
    public static <T> Iterable<T> in(final Iterator<T> iterator) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return iterator;
            }
        };
    }

    
Simple syntactic sugar to provide some reading information to the code. You can use this method to embrace any argument when a method is called. For example:
 encode(licensePlain, using(signatureGenerator), to(outputStream));
 
(actual code was copied from License.java). to(java.lang.Object) is also a similar method.

Note that these methods increase the size of the VM code, but any modern Java implementation will optimize them off in the JIT phase.

Parameters:
<T>
t argument to pass back on to the called
Returns:
the argument as it is.
    public static <T> T using(T t) {
        return t;
    }

    
For documentation see using(java.lang.Object).

Parameters:
<T>
t
Returns:
    public static <T> T to(T t) {
        return t;
    }

    

Parameters:
string to match
regexSet regular expressions provided as set of strings
Returns:
true if the string matches any of the regular expressions
    public static boolean matchesAny(String stringSet<StringregexSet) {
        for (String regex : regexSet) {
            if (string.matches(regex)) {
                return true;
            }
        }
        return false;
    }
New to GrepCode? Check out our FAQ X