Advertisement
Barteks2x

Untitled

Jun 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.75 KB | None | 0 0
  1. /*
  2.  *  This file is part of Cubic Chunks Mod, licensed under the MIT License (MIT).
  3.  *
  4.  *  Copyright (c) 2015 contributors
  5.  *
  6.  *  Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  *  of this software and associated documentation files (the "Software"), to deal
  8.  *  in the Software without restriction, including without limitation the rights
  9.  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  *  copies of the Software, and to permit persons to whom the Software is
  11.  *  furnished to do so, subject to the following conditions:
  12.  *
  13.  *  The above copyright notice and this permission notice shall be included in
  14.  *  all copies or substantial portions of the Software.
  15.  *
  16.  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22.  *  THE SOFTWARE.
  23.  */
  24. package cubicchunks.util;
  25.  
  26. /*
  27.  * Minecraft Forge
  28.  * Copyright (c) 2018.
  29.  *
  30.  * This library is free software; you can redistribute it and/or
  31.  * modify it under the terms of the GNU Lesser General Public
  32.  * License as published by the Free Software Foundation version 2.1
  33.  * of the License.
  34.  *
  35.  * This library is distributed in the hope that it will be useful,
  36.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  38.  * Lesser General Public License for more details.
  39.  *
  40.  * You should have received a copy of the GNU Lesser General Public
  41.  * License along with this library; if not, write to the Free Software
  42.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  43.  */
  44.  
  45. import net.minecraftforge.registries.IForgeRegistry;
  46. import net.minecraftforge.registries.IForgeRegistryEntry;
  47. import net.minecraftforge.registries.RegistryManager;
  48.  
  49. import java.util.Objects;
  50. import java.util.Optional;
  51. import java.util.function.Consumer;
  52. import java.util.function.Function;
  53. import java.util.function.Predicate;
  54. import java.util.function.Supplier;
  55. import java.util.stream.Stream;
  56.  
  57. public final class RegistryObject<T extends IForgeRegistryEntry<? super T>>
  58. {
  59.     private final String name;
  60.     private T value;
  61.     private boolean searched;
  62.     private final IForgeRegistry<? super T> owningRegistry;
  63.  
  64.     public static <T extends IForgeRegistryEntry<T>, U extends T> RegistryObject<U> of(final String name, Class<T> type) {
  65.         return new RegistryObject<U>(name, type);
  66.     }
  67.  
  68.     private static RegistryObject<?> EMPTY = new RegistryObject<>();
  69.  
  70.     private static <T extends IForgeRegistryEntry<? super T>> RegistryObject<T> empty() {
  71.         @SuppressWarnings("unchecked")
  72.         RegistryObject<T> t = (RegistryObject<T>) EMPTY;
  73.         return t;
  74.     }
  75.  
  76.     private RegistryObject() {
  77.         this.searched = true;
  78.         this.name = "";
  79.         this.owningRegistry = null;
  80.     }
  81.  
  82.     private <V extends IForgeRegistryEntry<V>> RegistryObject(String name, Class<V> clazz)
  83.     {
  84.         this.name = name;
  85.         this.owningRegistry = (IForgeRegistry) RegistryManager.ACTIVE.getRegistry(clazz);
  86.     }
  87.  
  88.     private T getValue()
  89.     {
  90.         if (!searched) {
  91.  
  92.             searched = true;
  93.         }
  94.         return this.value;
  95.     }
  96.  
  97.     public String getName() {
  98.         return this.name;
  99.     }
  100.  
  101.     public Stream<T> stream() {
  102.         return isPresent() ? Stream.of(getValue()) : Stream.of();
  103.     }
  104.     /**
  105.      * Return {@code true} if there is a mod object present, otherwise {@code false}.
  106.      *
  107.      * @return {@code true} if there is a mod object present, otherwise {@code false}
  108.      */
  109.     public boolean isPresent() {
  110.         return getValue() != null;
  111.     }
  112.  
  113.     /**
  114.      * If a mod object is present, invoke the specified consumer with the object,
  115.      * otherwise do nothing.
  116.      *
  117.      * @param consumer block to be executed if a mod object is present
  118.      * @throws NullPointerException if mod object is present and {@code consumer} is
  119.      * null
  120.      */
  121.     public void ifPresent(Consumer<? super T> consumer) {
  122.         if (getValue() != null)
  123.             consumer.accept(getValue());
  124.     }
  125.  
  126.     /**
  127.      * If a mod object is present, and the mod object matches the given predicate,
  128.      * return an {@code RegistryObject} describing the value, otherwise return an
  129.      * empty {@code RegistryObject}.
  130.      *
  131.      * @param predicate a predicate to apply to the mod object, if present
  132.      * @return an {@code RegistryObject} describing the value of this {@code RegistryObject}
  133.      * if a mod object is present and the mod object matches the given predicate,
  134.      * otherwise an empty {@code RegistryObject}
  135.      * @throws NullPointerException if the predicate is null
  136.      */
  137.     public RegistryObject<T> filter(Predicate<? super T> predicate) {
  138.         Objects.requireNonNull(predicate);
  139.         if (!isPresent())
  140.             return this;
  141.         else
  142.             return predicate.test(getValue()) ? this : empty();
  143.     }
  144.  
  145.     /**
  146.      * If a mod object is present, apply the provided mapping function to it,
  147.      * and if the result is non-null, return an {@code Optional} describing the
  148.      * result.  Otherwise return an empty {@code Optional}.
  149.      *
  150.      * @apiNote This method supports post-processing on optional values, without
  151.      * the need to explicitly check for a return status.
  152.      *
  153.      * @param <U> The type of the result of the mapping function
  154.      * @param mapper a mapping function to apply to the mod object, if present
  155.      * @return an {@code Optional} describing the result of applying a mapping
  156.      * function to the mod object of this {@code RegistryObject}, if a mod object is present,
  157.      * otherwise an empty {@code Optional}
  158.      * @throws NullPointerException if the mapping function is null
  159.      */
  160.     public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
  161.         Objects.requireNonNull(mapper);
  162.         if (!isPresent())
  163.             return Optional.empty();
  164.         else {
  165.             return Optional.ofNullable(mapper.apply(getValue()));
  166.         }
  167.     }
  168.  
  169.     /**
  170.      * If a value is present, apply the provided {@code Optional}-bearing
  171.      * mapping function to it, return that result, otherwise return an empty
  172.      * {@code Optional}.  This method is similar to {@link #map(Function)},
  173.      * but the provided mapper is one whose result is already an {@code Optional},
  174.      * and if invoked, {@code flatMap} does not wrap it with an additional
  175.      * {@code Optional}.
  176.      *
  177.      * @param <U> The type parameter to the {@code Optional} returned by
  178.      * @param mapper a mapping function to apply to the mod object, if present
  179.      *           the mapping function
  180.      * @return the result of applying an {@code Optional}-bearing mapping
  181.      * function to the value of this {@code Optional}, if a value is present,
  182.      * otherwise an empty {@code Optional}
  183.      * @throws NullPointerException if the mapping function is null or returns
  184.      * a null result
  185.      */
  186.     public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
  187.         Objects.requireNonNull(mapper);
  188.         if (!isPresent())
  189.             return Optional.empty();
  190.         else {
  191.             return Objects.requireNonNull(mapper.apply(getValue()));
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * Return the mod object if present, otherwise return {@code other}.
  197.      *
  198.      * @param other the mod object to be returned if there is no mod object present, may
  199.      * be null
  200.      * @return the mod object, if present, otherwise {@code other}
  201.      */
  202.     public T orElse(T other) {
  203.         return getValue() != null ? getValue() : other;
  204.     }
  205.  
  206.     /**
  207.      * Return the mod object if present, otherwise invoke {@code other} and return
  208.      * the result of that invocation.
  209.      *
  210.      * @param other a {@code Supplier} whose result is returned if no mod object
  211.      * is present
  212.      * @return the mod object if present otherwise the result of {@code other.get()}
  213.      * @throws NullPointerException if mod object is not present and {@code other} is
  214.      * null
  215.      */
  216.     public T orElseGet(Supplier<? extends T> other) {
  217.         return getValue() != null ? getValue() : other.get();
  218.     }
  219.  
  220.     /**
  221.      * Return the contained mod object, if present, otherwise throw an exception
  222.      * to be created by the provided supplier.
  223.      *
  224.      * @apiNote A method reference to the exception constructor with an empty
  225.      * argument list can be used as the supplier. For example,
  226.      * {@code IllegalStateException::new}
  227.      *
  228.      * @param <X> Type of the exception to be thrown
  229.      * @param exceptionSupplier The supplier which will return the exception to
  230.      * be thrown
  231.      * @return the present mod object
  232.      * @throws X if there is no mod object present
  233.      * @throws NullPointerException if no mod object is present and
  234.      * {@code exceptionSupplier} is null
  235.      */
  236.     public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
  237.         if (getValue() != null) {
  238.             return getValue();
  239.         } else {
  240.             throw exceptionSupplier.get();
  241.         }
  242.     }
  243.  
  244.     @Override
  245.     public boolean equals(Object obj)
  246.     {
  247.         if (this == obj) return true;
  248.         if (obj instanceof RegistryObject) {
  249.             return Objects.equals(((RegistryObject)obj).name, name);
  250.         }
  251.         return false;
  252.     }
  253.  
  254.     @Override
  255.     public int hashCode()
  256.     {
  257.         return Objects.hashCode(name);
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement