javascalautils.EitherCompanion Maven / Gradle / Ivy
/**
* Copyright 2015 Peter Nerg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javascalautils;
/**
* Acts as a Scala type companion object for {@link Left}/{@link Right}.
* The primary purpose is to get the Scala feel of instantiating classes.
* In Scala you can define a companion object for a class, acting as a static reference/singleton for that class allowing you do define factory methods.
* One use case is to define methods with the same name as the class and let these methods invoke the constructor thus creating a nice way to create instances
* without using the word "new".
* This can be achieved in java by statically importing a method and then using it.
* The limitation is that classes may not have method with the same name as the class itself hence new companion classes have to be created.
* To be able to use it in a neat concise way one needs to statically import the method.
*
*
* import static javascalautils.EitherCompanion.Left;
* import static javascalautils.EitherCompanion.Right;
*
* Either<InputStream,String> left = Left(new FileInputStream("foo.bar"));
* Either<InputStream,String> right = Right("Right is not Left");
*
*
*
*
* @author Peter Nerg
* @since 1.3
*/
public final class EitherCompanion {
private EitherCompanion() {
}
/**
* Creates an instance of {@link Left}.
* Best used in conjunction with statically importing this method.
*
*
* import static javascalautils.EitherCompanion.Left;
*
* Either<InputStream,String> left = Left(new FileInputStream("foo.bar"));
*
*
*
*
* @param
* The type for the {@link Left} side (not used for this method)
* @param
* The type for the {@link Right} side
* @param value
* The value represented by this Left
* @return The Left representing the value
* @see Left#Left(Object)
*/
public static Left Left(L value) {
return new Left<>(value);
}
/**
* Creates an instance of {@link Right}.
* Best used in conjunction with statically importing this method.
*
*
* import static javascalautils.EitherCompanion.Right;
*
* Either<InputStream,String> right = Right("Right is not Left");
*
*
*
*
* @param
* The type for the {@link Left} side (not used for this method)
* @param
* The type for the {@link Right} side
* @param value
* The value represented by this Right
* @return The Right representing the value
* @see Right#Right(Object)
*/
public static Right Right(R value) {
return new Right<>(value);
}
}