First Commit: Moving the project from google code to github

This commit is contained in:
Abhinav Sarkar 2010-06-02 01:18:32 +05:30
commit 6a3ae6ac23
11 changed files with 569 additions and 0 deletions

9
.classpath Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.settings/*
target/*

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>nulllessj</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

17
nulllessj.iml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/annotations" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target/classes" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.4" level="project" />
</component>
</module>

40
pom.xml Normal file
View File

@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.abhinavsarkar</groupId>
<artifactId>nulllessj</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>nulllessj</name>
<url>http://github.com/abhin4v/nulllessj</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,26 @@
package net.abhinavsarkar.nulllessj;
import java.util.concurrent.Callable;
/**
* A {@link Callable} class with context.
*
* @author Abhinav Sarkar <abhinav@abhinavsarkar.net>
*
* @param <T> type of the context
* @param <V> type of the return value of the callable
*/
public abstract class ContextualCallable<T, V> implements Callable<V> {
protected T context;
/**
* Set the context.
*
* @param context the context
*/
public void setContext(final T context) {
this.context = context;
}
}

View File

@ -0,0 +1,23 @@
package net.abhinavsarkar.nulllessj;
/**
* A {@link Runnable} class with context.
*
* @author Abhinav Sarkar <abhinav@abhinavsarkar.net>
*
* @param <T> type of the context
*/
public abstract class ContextualRunnable<T> implements Runnable {
protected T context;
/**
* Set the context.
*
* @param context the context
*/
public void setContext(final T context) {
this.context = context;
}
}

View File

@ -0,0 +1,64 @@
package net.abhinavsarkar.nulllessj;
public final class Just<T> extends Maybe<T> {
private final T value;
public Just(final T value) {
if (value == null) {
throw new NothingException();
}
this.value = value;
}
public static <T> Just<T> just(final T value) {
return new Just<T>(value);
}
@Override
public T get() {
return value;
}
@Override
public boolean isNothing() {
return false;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Just<T> other = (Just<T>) obj;
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}
@Override
public String toString() {
return value.toString();
}
}

View File

@ -0,0 +1,317 @@
package net.abhinavsarkar.nulllessj;
import static net.abhinavsarkar.nulllessj.Just.just;
import java.util.concurrent.Callable;
/**
* A container class for representing a value which 'maybe' present.
* This is an abstract class with two concrete subclasses: {@link Just}
* and {@link Nothing} which represent a present and non-present value
* respectively.
*
* @author Abhinav Sarkar <abhinav@abhinavsarkar.net>
*
* @param <T> Type of the object being wrapped.
*/
public abstract class Maybe<T> {
/**
* Returns if the class is {@link Nothing}
*/
abstract public boolean isNothing();
/**
* Returns if the class is {@link Just}
*/
public boolean isSomething() {
return !isNothing();
}
/**
* A convenience shortcut for {@link Maybe#get()}
*/
public T $() {
return get();
}
/**
* Returns the original wrapped value if the class is {@link Just}.
* Throws {@link NothingException} if the class is {@link Nothing}.
*/
abstract public T get();
/**
* Returns the original wrapped value if the class is {@link Just}.
* Returns the other parameter if the class is {@link Nothing}.
*
* @param other The value to return if the class is {@link Nothing}.
*/
public T getOrElse(final T other) {
if (isNothing()) {
return other;
} else {
return get();
}
}
/**
* Calls the callable provided if the class is {@link Nothing} and returns
* the result of the call wrapped in {@link Maybe}.
* If {@link NothingException} or {@link NullPointerException} is thrown
* while calling the callable, then returns an object of type
* {@link Nothing}.
* If any other exception is thrown while calling the callable, then the
* exception is caught and rethrown wrapped in a {@link RuntimeException}.
*
* @param <V> Return type of the callable provided.
* @param callable The callable to call
*
* @throws RuntimeException See the description.
*
* @see Maybe#maybe
*/
public <V> Maybe<V> callIfNothing(final Callable<V> callable) {
if (isNothing()) {
try {
return maybe(callable.call());
} catch (NothingException e) {
return new Nothing<V>();
} catch (NullPointerException e) {
return new Nothing<V>();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return new Nothing<V>();
}
}
/**
* Runs the runnable provided if the class is {@link Nothing}.
* If {@link NothingException} or {@link NullPointerException} is thrown
* while running the runnable, then the execution of the runnable terminates
* silently and this method returns.
* If any other exception is thrown while running the runnable, then the
* exception is caught and rethrown wrapped in a {@link RuntimeException}.
*
* @param runnable
*
* @throws RuntimeException See the description.
*/
public void runIfNothing(final Runnable runnable) {
if (isNothing()) {
try {
runnable.run();
} catch (NothingException e) {
return;
} catch (NullPointerException e) {
return;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/**
* Calls the callable provided if the class is {@link Just} and returns
* the result of the call wrapped in {@link Maybe}.
* The callable is provided the wrapped value as the context.
* If {@link NothingException} or {@link NullPointerException} is thrown
* while calling the callable, then returns an object of type
* {@link Nothing}.
* If any other exception is thrown while calling the callable, then the
* exception is caught and rethrown wrapped in a {@link RuntimeException}.
*
* @param <V> Return type of the callable provided.
* @param callable The callable to call
*
* @throws RuntimeException See the description.
*
* @see Maybe#maybe
*/
public <V> Maybe<V> callIfSomething(final ContextualCallable<T,V> callable) {
if (isSomething()) {
callable.setContext(get());
try {
return maybe(callable.call());
} catch (NothingException e) {
return new Nothing<V>();
} catch (NullPointerException e) {
return new Nothing<V>();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return new Nothing<V>();
}
}
/**
* Calls the <code>somethingCallable</code> callable or the
* <code>nothingCallable</code> callable if the class is {@link Just}
* or {@link Nothing} respectively.
* Returns the result of the call wrapped in {@link Maybe}.
* The <code>somethingCallable</code> is provided the wrapped value as the context.
* If {@link NothingException} or {@link NullPointerException} is thrown
* while calling the callables, then returns an object of type
* {@link Nothing}.
* If any other exception is thrown while calling the callables, then the
* exception is caught and rethrown wrapped in a {@link RuntimeException}.
*
* @param <V> Return type of the callables provided.
* @param somethingCallable The callable to call if the class in {@link Just}
* @param nothingCallable The callable to call if the class in {@link Nothing}
*
* @throws RuntimeException See the description.
*
* @see Maybe#maybe
*/
public <V> Maybe<V> callIfSomethingOrElse(
final ContextualCallable<T,V> somethingCallable,
final Callable<V> nothingCallable) {
if (isNothing()) {
return callIfNothing(nothingCallable);
} else {
return callIfSomething(somethingCallable);
}
}
/**
* Runs the <code>runnable</code> provided if the class is {@link Just}.
* The <code>runnable</code> is provided the wrapped value as the context.
* If {@link NothingException} or {@link NullPointerException} is thrown
* while running the runnable, then the execution of the runnable terminates
* silently and this method returns.
* If any other exception is thrown while running the runnable, then the
* exception is caught and rethrown wrapped in a {@link RuntimeException}.
*
* @param runnable The runnable to run
*
* @throws RuntimeException See the description.
*/
public void runIfSomething(final ContextualRunnable<T> runnable) {
if (isSomething()) {
runnable.setContext(get());
try {
runnable.run();
} catch (NothingException e) {
return;
} catch (NullPointerException e) {
return;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/**
* Runs the <code>somethingRunnable</code> runnable or the
* <code>nothingRunnable</code> runnable if the class is {@link Just} or
* {@link Nothing} respectively.
* The <code>somethingRunnable</code> is provided the wrapped value as the context.
* If {@link NothingException} or {@link NullPointerException} is thrown
* while running the runnable, then the execution of the runnable terminates
* silently and this method returns.
* If any other exception is thrown while running the runnable, then the
* exception is caught and rethrown wrapped in a {@link RuntimeException}.
*
* @param somethingRunnable The runnable to run if the class in {@link Just}
* @param nothingRunnable The runnable to run if the class in {@link Nothing}
*
* @throws RuntimeException See the description.
*/
public void runIfSomethingOrElse(
final ContextualRunnable<T> somethingRunnable,
final Runnable nothingRunnable) {
if (isNothing()) {
runIfNothing(nothingRunnable);
} else {
runIfSomething(somethingRunnable);
}
}
/**
* The kind of the {@link Maybe} value.
*/
public static enum MaybeKind {
SOME, NONE;
}
/**
* Returns the kind of the {@link Maybe} value
* @return the kind of the {@link Maybe} value
*/
public MaybeKind kind() {
return isNothing() ? MaybeKind.NONE : MaybeKind.SOME;
}
/**
* Runs the <code>runnable</code> provided.
* If {@link NothingException} or {@link NullPointerException} is thrown
* while running the <code>runnable</code>, then the execution of the
* <code>runnable</code> terminates silently and this method returns.
* If any other exception is thrown while running the <code>runnable</code>,
* then the exception is caught and rethrown wrapped in a {@link RuntimeException}.
*
* @param runnable The runnable to run
*
* @throws RuntimeException See the description.
*/
public static void run(final Runnable runnable) {
try {
runnable.run();
} catch (NothingException e) {
return;
} catch (NullPointerException e) {
return;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Calls the callable provided and returns the result of the call wrapped
* in {@link Maybe}.
* If {@link NothingException} or {@link NullPointerException} is thrown
* while calling the callable, then returns an object of type
* {@link Nothing}.
* If any other exception is thrown while calling the callable, then the
* exception is caught and rethrown wrapped in a {@link RuntimeException}.
*
* @param <V> Return type of the callable provided.
* @param callable The callable to call
*
* @throws RuntimeException See the description.
*
* @see Maybe#maybe
*/
public static <V> Maybe<V> call(final Callable<V> callable) {
try {
return maybe(callable.call());
} catch (NothingException e) {
return new Nothing<V>();
} catch (NullPointerException e) {
return new Nothing<V>();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Wraps an Object in {@link Maybe} and returns the wrapped Object.
* If obj is null then returns an instance of {@link Nothing}.
* Otherwise returns an instance of {@link Just} wrapped over obj.
*
* @param <V> The type of the object to wrap
* @param obj The object to wrap.
*/
public static <V> Maybe<V> maybe(final V obj) {
if (obj == null) {
return new Nothing<V>();
} else {
return just(obj);
}
}
}

View File

@ -0,0 +1,41 @@
package net.abhinavsarkar.nulllessj;
public final class Nothing<T> extends Maybe<T> {
public Nothing() {}
@Override
public T get() {
throw new NothingException();
}
@Override
public boolean isNothing() {
return true;
}
@Override
public int hashCode() {
return -999999;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
return true;
}
@Override
public String toString() {
return "<Nothing>";
}
}

View File

@ -0,0 +1,7 @@
package net.abhinavsarkar.nulllessj;
public class NothingException extends RuntimeException {
private static final long serialVersionUID = 1L;
}