|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Option.java | 0% | 13.6% | 33.3% | 14.6% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.tuple; | |
| 36 | ||
| 37 | import java.util.Comparator; | |
| 38 | ||
| 39 | import edu.rice.cs.plt.collect.CollectUtil; | |
| 40 | import edu.rice.cs.plt.collect.TotalOrder; | |
| 41 | import edu.rice.cs.plt.object.ObjectUtil; | |
| 42 | ||
| 43 | /** | |
| 44 | * A wrapper for optional values. This provides a strictly-typed alternative to using | |
| 45 | * {@code null} to represent the absence of a value. Options have two variants: "some" | |
| 46 | * and "none." The "some" case is represented by {@link Wrapper}s; the "none" case is | |
| 47 | * represented by the {@link Null} singleton. {@code Option} values may be decomposed | |
| 48 | * by invoking {@link #unwrap()} or {@link #unwrap(Object)}, or by using an | |
| 49 | * {@link OptionVisitor}. | |
| 50 | */ | |
| 51 | public abstract class Option<T> extends Tuple { | |
| 52 | ||
| 53 | 1485 | protected Option() {} |
| 54 | ||
| 55 | /** Calls the appropriate case in the visitor. */ | |
| 56 | public abstract <Ret> Ret apply(OptionVisitor<? super T, ? extends Ret> visitor); | |
| 57 | ||
| 58 | /** Determine whether this Option is a "some" case. Mutually exclusive with {@link #isNone}. */ | |
| 59 | public abstract boolean isSome(); | |
| 60 | ||
| 61 | /** Determine whether this Option is a "none" case. Mutually exclusive with {@link #isSome}. */ | |
| 62 | 669 | public final boolean isNone() { return !isSome(); } |
| 63 | ||
| 64 | /** | |
| 65 | * Get the value wrapped by this Option, or throw an {@link OptionUnwrapException} if there | |
| 66 | * is no wrapped value. | |
| 67 | */ | |
| 68 | public abstract T unwrap() throws OptionUnwrapException; | |
| 69 | ||
| 70 | /** Get the value wrapped by this Option, or {@code forNone} if there is no wrapped value. */ | |
| 71 | public abstract T unwrap(T forNone); | |
| 72 | ||
| 73 | ||
| 74 | /** Create a "some" case wrapper for the given value. */ | |
| 75 | 912 | public static <T> Option<T> some(T val) { return new Wrapper<T>(val); } |
| 76 | ||
| 77 | /** Return the "none" case singleton, cast to the appropriate type. */ | |
| 78 | 607 | @SuppressWarnings("unchecked") public static <T> Option<T> none() { |
| 79 | 607 | return (Option<T>) Null.INSTANCE; |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Treat a possibly-null value as an {@code Option}: if the value is {@code null}, produce | |
| 84 | * a "none"; otherwise, produce a "some" wrapping the value. | |
| 85 | */ | |
| 86 | 0 | @SuppressWarnings("unchecked") public static <T> Option<T> wrap(T val) { |
| 87 | 0 | if (val == null) { return (Option<T>) Null.INSTANCE; } |
| 88 | 0 | else { return new Wrapper<T>(val); } |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * A more general form of the instance method {@link #unwrap(Object)}, allowing {@code forNone} | |
| 93 | * to have a different type than {@code opt} (the result has a common supertype). | |
| 94 | */ | |
| 95 | 0 | public static <T> T unwrap(Option<? extends T> opt, T forNone) { |
| 96 | 0 | if (opt.isSome()) { return opt.unwrap(); } |
| 97 | 0 | else { return forNone; } |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * Produce a comparator for options, ordered by the natural order of the elements with "none" values at | |
| 102 | * the front. | |
| 103 | */ | |
| 104 | 0 | public static <T extends Comparable<? super T>> TotalOrder<Option<? extends T>> comparator() { |
| 105 | 0 | return new OptionComparator<T>(CollectUtil.<T>naturalOrder()); |
| 106 | } | |
| 107 | ||
| 108 | /** Produce a comparator for options, ordered by the given comparator with "none" values at the front. */ | |
| 109 | 0 | public static <T> TotalOrder<Option<? extends T>> comparator(Comparator<? super T> comp) { |
| 110 | 0 | return new OptionComparator<T>(comp); |
| 111 | } | |
| 112 | ||
| 113 | private static final class OptionComparator<T> extends TotalOrder<Option<? extends T>> { | |
| 114 | private final Comparator<? super T> _comp; | |
| 115 | 0 | public OptionComparator(Comparator<? super T> comp) { _comp = comp; } |
| 116 | 0 | public int compare(Option<? extends T> o1, Option<? extends T> o2) { |
| 117 | 0 | if (o1.isSome()) { return o2.isSome() ? _comp.compare(o1.unwrap(), o2.unwrap()) : 1; } |
| 118 | 0 | else { return o2.isSome() ? -1 : 0; } |
| 119 | } | |
| 120 | 0 | public boolean equals(Object o) { |
| 121 | 0 | if (this == o) { return true; } |
| 122 | 0 | else if (!(o instanceof OptionComparator<?>)) { return false; } |
| 123 | else { | |
| 124 | 0 | OptionComparator<?> cast = (OptionComparator<?>) o; |
| 125 | 0 | return _comp.equals(cast._comp); |
| 126 | } | |
| 127 | } | |
| 128 | 0 | public int hashCode() { return ObjectUtil.hash(OptionComparator.class, _comp); } |
| 129 | } | |
| 130 | ||
| 131 | } |
|
||||||||||