Created
December 30, 2015 18:43
-
-
Save elucash/7d151a06ddac4e46bc06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bbbb; | |
import org.immutables.value.Value; | |
@Value.Immutable | |
public interface Complex { | |
@Value.Parameter double re(); | |
@Value.Parameter double im(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bbbb; | |
import com.google.common.base.MoreObjects; | |
import com.google.common.base.Preconditions; | |
import com.google.common.collect.Lists; | |
import com.google.common.primitives.Doubles; | |
import java.util.List; | |
import javax.annotation.Generated; | |
import javax.annotation.Nullable; | |
import javax.annotation.ParametersAreNonnullByDefault; | |
import javax.annotation.concurrent.Immutable; | |
import javax.annotation.concurrent.NotThreadSafe; | |
/** | |
* Immutable implementation of {@link Complex}. | |
* <p> | |
* Use the builder to create immutable instances: | |
* {@code ImmutableComplex.builder()}. | |
* Use the static factory method to create immutable instances: | |
* {@code ImmutableComplex.of()}. | |
*/ | |
@SuppressWarnings("all") | |
@ParametersAreNonnullByDefault | |
@Generated({"Immutables.generator", "Complex"}) | |
@Immutable | |
public final class ImmutableComplex implements Complex { | |
private final double re; | |
private final double im; | |
private ImmutableComplex(double re, double im) { | |
this.re = re; | |
this.im = im; | |
} | |
/** | |
* @return The value of the {@code re} attribute | |
*/ | |
@Override | |
public double re() { | |
return re; | |
} | |
/** | |
* @return The value of the {@code im} attribute | |
*/ | |
@Override | |
public double im() { | |
return im; | |
} | |
/** | |
* Copy the current immutable object by setting a value for the {@link Complex#re() re} attribute. | |
* @param value A new value for re | |
* @return A modified copy of the {@code this} object | |
*/ | |
public final ImmutableComplex withRe(double value) { | |
double newValue = value; | |
return new ImmutableComplex(newValue, this.im); | |
} | |
/** | |
* Copy the current immutable object by setting a value for the {@link Complex#im() im} attribute. | |
* @param value A new value for im | |
* @return A modified copy of the {@code this} object | |
*/ | |
public final ImmutableComplex withIm(double value) { | |
double newValue = value; | |
return new ImmutableComplex(this.re, newValue); | |
} | |
/** | |
* This instance is equal to all instances of {@code ImmutableComplex} that have equal attribute values. | |
* @return {@code true} if {@code this} is equal to {@code another} instance | |
*/ | |
@Override | |
public boolean equals(@Nullable Object another) { | |
if (this == another) return true; | |
return another instanceof ImmutableComplex | |
&& equalTo((ImmutableComplex) another); | |
} | |
private boolean equalTo(ImmutableComplex another) { | |
return Double.doubleToLongBits(re) == Double.doubleToLongBits(another.re) | |
&& Double.doubleToLongBits(im) == Double.doubleToLongBits(another.im); | |
} | |
/** | |
* Computes a hash code from attributes: {@code re}, {@code im}. | |
* @return hashCode value | |
*/ | |
@Override | |
public int hashCode() { | |
int h = 31; | |
h = h * 17 + Doubles.hashCode(re); | |
h = h * 17 + Doubles.hashCode(im); | |
return h; | |
} | |
/** | |
* Prints the immutable value {@code Complex...} with all non-generated | |
* and non-auxiliary attribute values. | |
* @return A string representation of the value | |
*/ | |
@Override | |
public String toString() { | |
return MoreObjects.toStringHelper("Complex") | |
.add("re", re) | |
.add("im", im) | |
.toString(); | |
} | |
/** | |
* Construct a new immutable {@code Complex} instance. | |
* @param re The value for the {@code re} attribute | |
* @param im The value for the {@code im} attribute | |
* @return An immutable Complex instance | |
*/ | |
public static ImmutableComplex of(double re, double im) { | |
return new ImmutableComplex(re, im); | |
} | |
/** | |
* Creates an immutable copy of a {@link Complex} value. | |
* Uses accessors to get values to initialize the new immutable instance. | |
* If an instance is already immutable, it is returned as is. | |
* @param instance The instance to copy | |
* @return A copied immutable Complex instance | |
*/ | |
public static ImmutableComplex copyOf(Complex instance) { | |
if (instance instanceof ImmutableComplex) { | |
return (ImmutableComplex) instance; | |
} | |
return ImmutableComplex.builder() | |
.from(instance) | |
.build(); | |
} | |
/** | |
* Creates a builder for {@link bbbb.ImmutableComplex ImmutableComplex}. | |
* @return A new ImmutableComplex builder | |
*/ | |
public static ImmutableComplex.Builder builder() { | |
return new ImmutableComplex.Builder(); | |
} | |
/** | |
* Builds instances of type {@link bbbb.ImmutableComplex ImmutableComplex}. | |
* Initialize attributes and then invoke the {@link #build()} method to create an | |
* immutable instance. | |
* <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection, | |
* but instead used immediately to create instances.</em> | |
*/ | |
@NotThreadSafe | |
public static final class Builder { | |
private static final long INIT_BIT_RE = 0x1L; | |
private static final long INIT_BIT_IM = 0x2L; | |
private long initBits = 0x3; | |
private double re; | |
private double im; | |
private Builder() {} | |
/** | |
* Fill a builder with attribute values from the provided {@link Complex} instance. | |
* Regular attribute values will be replaced with those from the given instance. | |
* Absent optional values will not replace present values. | |
* @param instance The instance from which to copy values | |
* @return {@code this} builder for use in a chained invocation | |
*/ | |
public final Builder from(Complex instance) { | |
Preconditions.checkNotNull(instance, "instance"); | |
re(instance.re()); | |
im(instance.im()); | |
return this; | |
} | |
/** | |
* Initializes the value for the {@link Complex#re() re} attribute. | |
* @param re The value for re | |
* @return {@code this} builder for use in a chained invocation | |
*/ | |
public final Builder re(double re) { | |
this.re = re; | |
initBits &= ~INIT_BIT_RE; | |
return this; | |
} | |
/** | |
* Initializes the value for the {@link Complex#im() im} attribute. | |
* @param im The value for im | |
* @return {@code this} builder for use in a chained invocation | |
*/ | |
public final Builder im(double im) { | |
this.im = im; | |
initBits &= ~INIT_BIT_IM; | |
return this; | |
} | |
/** | |
* Builds a new {@link bbbb.ImmutableComplex ImmutableComplex}. | |
* @return An immutable instance of Complex | |
* @throws java.lang.IllegalStateException if any required attributes are missing | |
*/ | |
public ImmutableComplex build() | |
throws IllegalStateException { | |
checkRequiredAttributes(); return new ImmutableComplex(re, im); | |
} | |
private boolean reIsSet() { | |
return (initBits & INIT_BIT_RE) == 0; | |
} | |
private boolean imIsSet() { | |
return (initBits & INIT_BIT_IM) == 0; | |
} | |
private void checkRequiredAttributes() throws IllegalStateException { | |
if (initBits != 0) { | |
throw new IllegalStateException(formatRequiredAttributesMessage()); | |
} | |
} | |
private String formatRequiredAttributesMessage() { | |
List<String> attributes = Lists.newArrayList(); | |
if (!reIsSet()) attributes.add("re"); | |
if (!imIsSet()) attributes.add("im"); | |
return "Cannot build Complex, some of required attributes are not set " + attributes; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bbbb; | |
public class Use { | |
public static void main(String... args) { | |
Complex c1 = ImmutableComplex.builder() | |
.re(1) | |
.im(2) | |
.build(); | |
Complex c2 = ImmutableComplex.of(1d, 2d); | |
boolean truish = c1.equals(c2); | |
System.out.println(truish); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment