All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.timgroup.karg.reference.Refs Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
package com.timgroup.karg.reference;

import java.util.Iterator;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;

public final class Refs {
    
    public static  Iterable toIterable(final Ref ref) {
        return new Iterable() {
            @Override public Iterator iterator() {
                return new Iterator() {
                    private boolean consumed = ref.get() == null;
                    @Override public boolean hasNext() {
                        return !consumed;
                    }
    
                    @Override public T next() {
                        if (consumed) {
                            return null;
                        }
                        consumed = true;
                        return ref.get();
                    }
    
                    @Override public void remove() {
                        ref.set(null);
                    }
                };
            }
        };
    }
    
    public static  Function, Iterable> toIterable() {
        return new Function, Iterable>() {
            @Override public Iterable apply(Ref arg0) {
                return toIterable(arg0);
            }
        };
    }
    
    public static  Iterable toIterable(Iterable> refs) {
        Function, Iterable> toIterable = toIterable();
        return Iterables.concat(Iterables.transform(refs, toIterable));
    }
    
    private Refs() {  }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy