alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (PersistentHashSet.java)

This example Java source code file (PersistentHashSet.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

apersistentset, atransientset, duplicate, empty, ieditablecollection, illegalargumentexception, iobj, ipersistentcollection, ipersistentmap, ipersistentset, itransientcollection, itransientset, persistenthashset, transienthashset, util

The PersistentHashSet.java Java example source code

/**
 *   Copyright (c) Rich Hickey. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

/* rich Mar 3, 2008 */

package clojure.lang;

import java.util.List;

public class PersistentHashSet extends APersistentSet implements IObj, IEditableCollection {

static public final PersistentHashSet EMPTY = new PersistentHashSet(null, PersistentHashMap.EMPTY);

final IPersistentMap _meta;

public static PersistentHashSet create(Object... init){
	ITransientSet ret = (ITransientSet)EMPTY.asTransient();
	for(int i = 0; i < init.length; i++)
		{
		ret = (ITransientSet)ret.conj(init[i]);
		}
	return (PersistentHashSet)ret.persistent();
}

public static PersistentHashSet create(List init){
	ITransientSet ret = (ITransientSet)EMPTY.asTransient();
	for(Object key : init)
		{
		ret = (ITransientSet) ret.conj(key);
		}
	return (PersistentHashSet)ret.persistent();
}

static public PersistentHashSet create(ISeq items){
	ITransientSet ret = (ITransientSet)EMPTY.asTransient();
	for(; items != null; items = items.next())
		{
		ret = (ITransientSet) ret.conj(items.first());
		}
	return (PersistentHashSet)ret.persistent();
}

public static PersistentHashSet createWithCheck(Object... init){
    ITransientSet ret = (ITransientSet)EMPTY.asTransient();
	for(int i = 0; i < init.length; i++)
		{
		ret = (ITransientSet) ret.conj(init[i]);
		if(ret.count() != i + 1)
			throw new IllegalArgumentException("Duplicate key: " + init[i]);
		}
	return (PersistentHashSet) ret.persistent();
}

public static PersistentHashSet createWithCheck(List init){
    ITransientSet ret = (ITransientSet)EMPTY.asTransient();
	int i=0;
	for(Object key : init)
		{
		ret = (ITransientSet) ret.conj(key);
		if(ret.count() != i + 1)
			throw new IllegalArgumentException("Duplicate key: " + key);		
		++i;
		}
    return (PersistentHashSet) ret.persistent();
}

static public PersistentHashSet createWithCheck(ISeq items){
    ITransientSet ret = (ITransientSet)EMPTY.asTransient();
	for(int i=0; items != null; items = items.next(), ++i)
		{
		ret = (ITransientSet) ret.conj(items.first());
		if(ret.count() != i + 1)
			throw new IllegalArgumentException("Duplicate key: " + items.first());
		}
    return (PersistentHashSet) ret.persistent();
}

PersistentHashSet(IPersistentMap meta, IPersistentMap impl){
	super(impl);
	this._meta = meta;
}

public IPersistentSet disjoin(Object key) {
	if(contains(key))
		return new PersistentHashSet(meta(),impl.without(key));
	return this;
}

public IPersistentSet cons(Object o){
	if(contains(o))
		return this;
	return new PersistentHashSet(meta(),impl.assoc(o,o));
}

public IPersistentCollection empty(){
	return EMPTY.withMeta(meta());	
}

public PersistentHashSet withMeta(IPersistentMap meta){
	return new PersistentHashSet(meta, impl);
}

public ITransientCollection asTransient() {
	return new TransientHashSet(((PersistentHashMap) impl).asTransient());
}

public IPersistentMap meta(){
	return _meta;
}

static final class TransientHashSet extends ATransientSet {
	TransientHashSet(ITransientMap impl) {
		super(impl);
	}

	public IPersistentCollection persistent() {
		return new PersistentHashSet(null, impl.persistent());
	}
}

}

Other Java examples (source code examples)

Here is a short list of links related to this Java PersistentHashSet.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.