Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

David R. MacIver http://unenterprise.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS 

Creating a circular reference between two objects in Java

This is in some sense the 'right' way of doing it as far as I can figure. Nothing else I've been able to come up with works even close to as well.

public class LazyModules 
{
  static int i = 0;

  static abstract class A{
    abstract B getB();
    int k = i++;
    public String toString() { return "" + k; }
  }

  static abstract class B{
    abstract A getA();
    int k = i++;
    public String toString() { return "" + k; }
  }

  public static void doStuff(A foo, B bar){
    System.out.println("foo = " + foo);
    System.out.println("foo.b = " + foo.getB());
    System.out.println("bar = " + bar);
    System.out.println("bar.a = " + bar.getA()); 
  }

  public static void main(String[] args){
    new Object(){
      final A foo = new A() { B getB() { return bar; }  };
      final B bar = new B() { A getA() { return foo; }  };

      { doStuff(foo, bar); }
    };
  }
}

Simple CharMap based on a Radix binary tree

Very simple Java implementation of a map from characters to objects based on a radix tree.

It's not actually that fast, as I've not bothered to optimise it at all (a brief attempt didn't work very well and I lost interest after that). It seems to be a little slower than a HashMap using boxed Characters and a little faster than a TreeMap (when the JIT gets up to speed anyway. Before the JIT has done any optimisation the hashmap is 2 or 3 times faster). I might at some point implement a HashMap using unboxed characters for keys to see what kind of performance improvement it gives.

Using a large array is of course faster than all of the options by an order of magnitude, but has the downside of being a bit too large to be useful. :-)

public class RadixCharMap<T>
{
    private RadixCharMap<T> map0; 
    private RadixCharMap<T> map1;
    private T value;
    
    private RadixCharMap<T> getMap(int i){
        if (i > 0) return map1 == null ? (map1 = new RadixCharMap<T>()) : map1;
        else return map0 == null ? (map0 = new RadixCharMap<T>()) : map0;}    
    
    public RadixCharMap<T> put(char key, T value){
        if (key == '\0') this.value = value;
        else getMap(key & 1).put((char)(key >> 1), value);
        return this;}
    
    public T get(char key){
        RadixCharMap current = this;
        if (key == '\0') return this.value;
        else return getMap(key & 1).get((char)(key >> 1));}
}

Goto in Java

After my C finite state machine, I figured I'd sink to new levels of depravity by figuring out how to write GOTO in Java. Here's the result.

Usual disclaimer about "if you use this code then the baby Jesus will cut kittens".

Update: Thanks to roots_ on freenode ##java for the suggestion of using a do { } while(false) instead, and cybereal for pointing out that I didn't need the label.

public class Goto
{
    public static int END = Integer.MAX_VALUE;

    public static void main(String[] args)
    {
        int _goto = 0;

        do
        {
            switch(_goto)
            {
                case 0:
                case 1:
                    System.out.println("Foo");
                    _goto = 3;
                    continue;
                
                case 2:
                     System.out.println("Baz");
                    _goto = END;
                    continue;
                case 3:
                     System.out.println("Bar");
                    _goto = 2;
                    continue;
             }
        } while(false)
    }
}

Class for faking Using in Java

This is a class for faking the 'using' syntax in Java. The idea is that you subclass it to provide your resource initialisation and finalisation code, providing protected final methods for resource access you don't want to be publically available. Users then create an anonymous inner class that overrides the body() method and has access to these resources only within the method. Sensitive resources should throw an exception if invoked when isLive returns false.

Now updated to include significantly less evil. (It no longer needs the _return method...)


public abstract class Use<T>
{
        private boolean live = false;

        protected final void isLive()
        {
            return live;
        } 

	/**
	 * Override this to provide initialisation code for this Use class. This will always be called before
	 * the body is executed.
	 */
	protected void start()
	{
	}

	/**
	 * Override this to provide finalisation code for this Use class. This will always be called at the end
	 * of use() execution.
	 */
	protected void finish()
	{
	}

	/**
	 * The body of the code to execute. Returning a value from this should execute _return(T value).
         */
	protected abstract T body();

	/**
	 * Returns body(), or null if this was never invoked. This will call
	 * all neccessary initialisation and finalisation code.
 	 */
	public final T use()
	{
		try
		{
			start();
                        live = true;

			return body();
		}
		finally
		{
                        live = false;
			finish();
		}
	}

}

2 + 2 = 5

Here is some very exciting Java code for printing 5.

import java.security.*;
import java.lang.reflect.*;
import java.util.*;

public class Test
{
    public static void main(String[] args) throws Exception
    {   
        AccessController.doPrivileged( new PrivilegedAction<Object>(){ public Object run(){try {
        Field field = Class.forName("java.lang.Integer$IntegerCache").getDeclaredFields()[0];
        field.setAccessible(true);

        Integer[] cache = (Integer[])field.get(null);
            
        cache[130] = 3;  
            
        Integer foo = 2;
 
        System.out.println(foo + 2); 
            
        return null;} catch(Exception e) { throw new RuntimeException(e); } } }); 
    }


}

Reading a webpage in Java

A trivial piece of example code demonstrating how to get a BufferedReader from a Url as a String and do something with it. This code simply prints the contents of the website at the first argument to stdout.

import java.io.*;
import java.net.URL;

public class WebsiteReader
{
	public static BufferedReader read(String url) throws Exception{
		return new BufferedReader(
			new InputStreamReader(
				new URL(url).openStream()));}

	public static void main (String[] args) throws Exception{
		BufferedReader reader = read(args[0]);
		String line = reader.readLine();

		while (line != null) {
			System.out.println(line);
			line = reader.readLine(); }}
}

Webcrawler

The core classes from my webcrawler implementation.

Again, not really working code as there are a bunch of dependencies missing. This is really for demonstration purposes.

public class WebCrawler<T extends WebPage> implements Iterable<T>
{    
    private final HashSet<T> visitedPages = new HashSet<T>();
    private final LinkedList<Object> workQueue = new LinkedList<Object>();
    private final PageProcessor<T> processor;
            
    // Map of URLs to pages.
    private final Map<String, WebPage> pages = new HashMap<String, WebPage>();     
    
    private final Predicate<Object> unvisited = new Predicate<Object>() { 
        public boolean satisfies(Object page){
            return !WebCrawler.this.visitedPages.contains(page);}};    

            
    public WebCrawler(PageProcessor<T> processor, String... urls){
        this.processor = processor;
        
        for (String url : urls){
            this.workQueue.add(processor.page(url));}}
    
    /* Iterator which iterates over all WebPages that haven't yet been visited.
     * It is thoroughly lazy and a web page will never be visited until it turns
     * up in this iterator.*/
    public final Iterator<T> pageIterator = (Iterator<T>)
        new FlatteningIterator(
            new ListeningIterator<Object>(
                    new FilterIterator<Object>(
                        unvisited, 
                        new PoppingIterator<Object>(this.workQueue))){
                @Override public void onNext(Object next){
                    if (next instanceof WebPage){
                        WebCrawler.this.visitedPages.add((T)next);
                        WebCrawler.this.workQueue.add(
                            new FilterIterator(unvisited, WebCrawler.this.processor.linkedPages((WebPage)next)));}}});
                    
    public Iterator<T> iterator(){ return IteratorUtils.link(this.visitedPages.iterator(), this.pageIterator); }                        
}

/**
 * Abstract class representing a mechanism for processing urls into pages. Contains 
 * utility methods and a cacheing strategy.
 *
 * @author david
 */
public abstract class PageProcessor<T extends WebPage> implements Transformer<String, T>
{
    private final PageCache<T> cache;
    private final IteratorTransformer<String, T> iteratorTransformer = new IteratorTransformer<String, T>(this);
    private Predicate<String> domain;
    
    public PageProcessor(Predicate<String> domain, PageCache<T> cache){
        this.domain = domain;
        this.cache = cache;}
    
    public PageProcessor(String domainPrefix, PageCache<T> cache){
        this(StringUtils.startsWith(domainPrefix), cache);}
    
    /**
     * Take the Url and return a WebPage corresponding to it.
     */
    protected abstract T process(String url);
        
    public T transform(String url){ return this.page(url); }
    
    /**
     * If the page has previously been processed, retrieve it from the internal cache.
     * Else process it and put it in the eternal cache.
     */
    public T page(String url){
        T page = cache.getCachedPage(url);
        
        if (page == null){
                page = this.process(url);
                cache.cachePage(page);}
        
        return page;}
    
    /**
     * Returns an iterator over all pages linked to by this page.
     */
    public Iterator<T> linkedPages(WebPage page){
        return iteratorTransformer.transform(new FilterIterator(domain, page.getLinkUrls()));}
}

/**
 * A very simple PageProcessor<WebPage> implementation based on the HTMLParser library
 * which uses a MapBackedPageCache.
 *
 * @author david
 */
public class HtmlParserPageProcessor extends PageProcessor<WebPage>
{       
    private static NodeFilter ALLOWED_TAGS = new NodeFilter(){
        public boolean accept(Node node){ 
            return (node instanceof LinkTag) || (node instanceof TitleTag);}};
    
    public HtmlParserPageProcessor(Predicate<String> domain){
        super(domain,  new MapBackedPageCache<WebPage>());}
            
    public HtmlParserPageProcessor(String domain){
        super(domain,  new MapBackedPageCache<WebPage>());}
                   
    /**
     * Fetches the resource represented by the URL, parses the HTML and extracts
     * the title element and all the links and uses them to build a WebPage object.
     */
    public WebPage process(String url){
        try{
            Parser parser = new Parser(url);
            NodeIterator iterator = parser.parse(ALLOWED_TAGS).elements();
        
            String title = "";
            List<String> links = new ArrayList<String>();
        
            while (iterator.hasMoreNodes()){
                Node node = iterator.nextNode();
                
                if (node instanceof TitleTag) title = ((TitleTag)node).getTitle();
                else if (node instanceof LinkTag) links.add(((LinkTag)node).extractLink());}
            return new WebPage(url, title, links);}               
        catch (Exception e){ throw new RuntimeException(e); }}
}

Recursively listing all files below a directory

// description of your code here
Simple iterator to iterate over all non-directory files the lie below a given directory. Demonstrates a use case for the FlatteningIterator snippet.


package playground.library.files;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Iterator;
import playground.library.functional.iterator.FlatteningIterator;

/**
 * Iterates over all non-directory files contained in some subdirectory of the 
 * current one.
 *
 * @author david
 */
public class RecursiveFileListIterator implements Iterator<File>
{
    private final FlatteningIterator flatteningIterator;
    
    public void remove() { } 
    
    public RecursiveFileListIterator(File file, FileFilter filter){
        this.flatteningIterator = new FlatteningIterator(new FileIterator(file, filter)); }
    
    public RecursiveFileListIterator(File file){
        this(file, null);}
    
    
    public boolean hasNext(){ 
        return flatteningIterator.hasNext();}
    
    public File next(){ 
        return (File)flatteningIterator.next();}
    
    
    /**
     * Iterator to iterate over all the files contained in a directory. It returns
     * a File object for non directories or a new FileIterator obejct for directories.
     */
    private static class FileIterator implements Iterator<Object>
    {
        private final Iterator<File> files;
        private final FileFilter filter;
        
        FileIterator(File file, FileFilter filter){ 
            this.files = Arrays.asList(file.listFiles(filter)).iterator();
            this.filter = filter;}
        
        public void remove() { }
        
        public Object next(){
            File next = this.files.next();
            
            if (next.isDirectory()) return new FileIterator(next, this.filter);
            else return next;}
        
        public boolean hasNext(){
            return this.files.hasNext();}       
    }
}

Functional JarClassLoader - warning, really awful

Some really awful functional style Java...

Note that this depends on a bunch of other classes in the playground namespace. If you actually want to use it ask me and I'll send you the source - I didn't really feel like snippetting all the needed code.

package playground.library.classloader;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import playground.library.functional.FunctionalUtils;
import playground.library.functional.Pair;
import playground.library.functional.iterator.FilterIterator;
import playground.library.functional.iterator.IteratorTransformer;
import playground.library.functional.Predicate;
import playground.library.functional.Transformer;
import playground.library.functional.iterator.DelegatingIterator;
import playground.library.functional.iterator.LinkingIterator;
import playground.library.jar.JarEntryIterator;
import playground.library.utils.IteratorUtils;
import playground.library.utils.IOUtils;

/**
 * ClassLoader for loading from Jar files.
 *
 * @author david
 */
public class JarClassLoader extends ClassLoader
{
    public JarClassLoader(ClassLoader parent) { super(parent); }    
    public JarClassLoader()                   { super(); }
    
    private final List<JarFile>     files = new ArrayList<JarFile>();
    
    /**
     * Provides an iterator over all pairs of (file, entry) of JarFiles associated
     * with this class loader and entries of those jar files.
     */
    private Iterator<Pair<JarFile, JarEntry>> getResources(){
        return new LinkingIterator<Pair<JarFile, JarEntry>>(
            new IteratorTransformer<JarFile, Iterator<Pair<JarFile,JarEntry>>>(
                new Transformer<JarFile, Iterator<Pair<JarFile, JarEntry>>>(){
                public Iterator<Pair<JarFile, JarEntry>> transform(JarFile file){
                    return FunctionalUtils.merge(file, new JarEntryIterator(file));}})
           .transform(JarClassLoader.this.files.iterator()));}
    
    /**
     * Searches through the available jar files to try and find a class with the given name.
     */
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException{
        final Iterator<Pair<JarFile,JarEntry>> resources = this.getResources();
        while (resources.hasNext()){
            final Pair<JarFile, JarEntry> resource = resources.next();
                if(resource.second.getName().equals(name)){
                    try { return this.load(resource.first.getInputStream(resource.second));}
                    catch (IOException e) { e.printStackTrace(); }}}
        throw new ClassNotFoundException();}
    
    /**
     * Loads the contents of the InputStream as a class. This doesn't validate
     * the contents, so will fail messily if you pass it something that isn't a
     * valid class file.
     */
    private Class<?> load(final InputStream input){
        try {
            final ByteArrayOutputStream output = new ByteArrayOutputStream();
            final byte[] bytes = IOUtils.copy(input, new ByteArrayOutputStream()).toByteArray();
            return this.defineClass(null, bytes, 0, bytes.length); }
        catch (IOException e){ throw new RuntimeException(e); }
        finally{
            try{ input.close(); }
            catch (IOException e){ throw new RuntimeException(e);}}}
        
    /**
     * Provides an iterator which lazily loads the contents of the jar file as
     * classes. Will also register the jar file with the class loader's file list.
     * When the iterator is exhausted the jar file will be removed from the file list.
     */
    public Iterator<Class<?>> load(final JarFile file) throws IOException{
        this.files.add(file);
                
        return  
            new DelegatingIterator<Class<?>>(
                new IteratorTransformer<JarEntry, Class<?>>(
                    new Transformer<JarEntry, Class<?>>(){
                        public Class transform(JarEntry stream){
                            InputStream fileStream = null;

                            try{return JarClassLoader.this.load(fileStream = file.getInputStream(stream)); }
                            catch(IOException e){throw new RuntimeException(e);}
                            finally{IOUtils.close(fileStream); }}})
                .transform(
                    new FilterIterator( 
                       new Predicate<JarEntry>(){ 
                           public boolean satisfies(JarEntry entry){
                               return entry.getName().endsWith(".class");}},
                       new JarEntryIterator(file)))){
                @Override
                public Class<?> next(){
                    final Class<?> clazz = super.next();
                    if (!this.hasNext()) JarClassLoader.this.files.remove(file);                    
                    return clazz;}
                
                @Override
                public boolean hasNext(){
                    final boolean hasNext = super.hasNext();
                    if (!hasNext) JarClassLoader.this.files.remove(file);
                    return hasNext;}};}

    /**
     * Loads all class files from the jar and returns a set of them.
     */
    public Set<Class<?>> loadAll(final JarFile file) throws IOException
    {
        return IteratorUtils.fill(new HashSet<Class<?>>(), this.load(file));
    }
}

Flattening iterator

A trivial utility class for iterating through a collection of objects in a 'flat' manner, descending into any collections (in this case defined as iterators, iterables or arrays, rather than elements of the Collection interface ) it finds and iterating through their elements. This preserves order, so {a, b, {c, d, {e}}} will be iterated through as a, b, c, d, e.

It's not very complicated, but the implementation amused me so I thought I'd share it.

package playground.library.functional.iterator;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Stack;

/**
 * An iterator that 'flattens out' collections, iterators, arrays, etc. 
 *
 * That is it will iterate out their contents in order, descending into any 
 * iterators, iterables or arrays provided to it.
 *
 * An example (not valid Java for brevity - some type declarations are ommitted):
 *
 * new FlattingIterator({1, 2, 3}, {{1, 2}, {3}}, new ArrayList({1, 2, 3}))
 *
 * Will iterate through the sequence 1, 2, 3, 1, 2, 3, 1, 2, 3.
 *
 * Note that this implements a non-generic version of the Iterator interface so
 * may be cast appropriately - it's very hard to give this class an appropriate 
 * generic type.
 *
 * @author david
 */
public class FlatteningIterator implements Iterator
{
    // Marker object. This is never exposed outside this class, so can be guaranteed
    // to be != anything else. We use it to indicate an absense of any other object.
    private final Object blank = new Object();
    
    /* This stack stores all the iterators found so far. The head of the stack is
     * the iterator which we are currently progressing through */
    private final Stack<Iterator<?>> iterators = new Stack<Iterator<?>>();
    
    // Storage field for the next element to be returned. blank when the next element
    // is currently unknown.
    private Object next = blank;
        
    public FlatteningIterator(Object... objects){
        this.iterators.push(Arrays.asList(objects).iterator());}
    
    public void remove(){
        /* Not implemented */}
    
    private void moveToNext(){
        if ((next == blank) && !this.iterators.empty() ) {
            if (!iterators.peek().hasNext()){
                iterators.pop();
                moveToNext();}
                else{
                    final Object next = iterators.peek().next();
                    if (next instanceof Iterator){
                      iterators.push((Iterator<?>)next);
                      moveToNext();}
                    else if (next instanceof Iterable){
                       iterators.push(((Iterable)next).iterator());
                       moveToNext();}
                    else if (next instanceof Array){
                        iterators.push(Arrays.asList((Array)next).iterator());
                        moveToNext();}
                    else this.next = next;}}}
    
    /**
     * Returns the next element in our iteration, throwing a NoSuchElementException
     * if none is found. 
     */
    public Object next() {
        moveToNext();
        
        if (this.next == blank) throw new NoSuchElementException();
        else{
            Object next = this.next;
            this.next = blank;
            return next;
        }}
    
    /**
     * Returns if there are any objects left to iterate over. This method 
     * can change the internal state of the object when it is called, but repeated
     * calls to it will not have any additional side effects.
     */
    public boolean hasNext(){
        moveToNext();
        return (this.next != blank);}    
}
« Newer Snippets
Older Snippets »
Showing 1-10 of 10 total  RSS