|
| 1 | +package io.foldright.inspectablewrappers; |
| 2 | + |
| 3 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 4 | +import edu.umd.cs.findbugs.annotations.Nullable; |
| 5 | + |
| 6 | +import java.util.Optional; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.function.Predicate; |
| 9 | + |
| 10 | +import static io.foldright.inspectablewrappers.InternalUtils.unwrapNonNull; |
| 11 | +import static java.util.Objects.requireNonNull; |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * This {@code Inspector} is used to inspect wrapper implementors: |
| 16 | + * <ul> |
| 17 | + * <li> |
| 18 | + * Reports whether any wrapper on the wrapper chain matches the given type by static method {@link #isInstanceOf(Object, Class)} |
| 19 | + * </li> |
| 20 | + * <li> |
| 21 | + * Retrieve the attachment from wrapper chain(wrapper instances implement interface {@link Wrapper}) |
| 22 | + * by static method {@link #getAttachment(Object, Object)}. |
| 23 | + * </li> |
| 24 | + * </ul> |
| 25 | + * |
| 26 | + * @see Wrapper |
| 27 | + */ |
| 28 | +public final class Inspector { |
| 29 | + |
| 30 | + // no need to create instance at all |
| 31 | + private Inspector() { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Reports whether any wrapper on the wrapper chain matches the given type. |
| 36 | + * <p> |
| 37 | + * The wrapper chain consists of wrapper itself, followed by the wrappers |
| 38 | + * obtained by repeatedly calling {@link Wrapper#unwrap()}. |
| 39 | + * |
| 40 | + * @param wrapper wrapper instance/wrapper chain |
| 41 | + * @param clazz target type |
| 42 | + * @param <W> the type of instances that be wrapped |
| 43 | + * @return return {@code false} if no wrapper on the wrapper chain matches the given type, |
| 44 | + * otherwise return {@code true} |
| 45 | + * @throws NullPointerException if any arguments is null or any wrapper {{@link Wrapper#unwrap()}} returns null |
| 46 | + * @see WrapperAdapter#adaptee() |
| 47 | + */ |
| 48 | + public static <W> boolean isInstanceOf(final W wrapper, final Class<?> clazz) { |
| 49 | + requireNonNull(wrapper, "wrapper is null"); |
| 50 | + requireNonNull(clazz, "clazz is null"); |
| 51 | + return inspect(wrapper, w -> clazz.isAssignableFrom(w.getClass())); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Reports whether any wrapper on the wrapper chain satisfy the given {@code predicate}. |
| 56 | + * <p> |
| 57 | + * The wrapper chain consists of wrapper itself, followed by the wrappers |
| 58 | + * obtained by repeatedly calling {@link Wrapper#unwrap()}. |
| 59 | + * |
| 60 | + * @param wrapper wrapper instance/wrapper chain |
| 61 | + * @param predicate inspect logic |
| 62 | + * @param <W> the type of instances that be wrapped |
| 63 | + * @return return {@code false} if no wrapper on the wrapper chain satisfy the given {@code predicate}, |
| 64 | + * otherwise return {@code true} |
| 65 | + * @throws NullPointerException if any arguments is null or any wrapper {{@link Wrapper#unwrap()}} returns null |
| 66 | + */ |
| 67 | + static <W> boolean inspect(final W wrapper, final Predicate<? super W> predicate) { |
| 68 | + requireNonNull(wrapper, "wrapper is null"); |
| 69 | + requireNonNull(predicate, "predicate is null"); |
| 70 | + return travel(wrapper, w -> { |
| 71 | + if (predicate.test(w)) return Optional.of(true); |
| 72 | + else return Optional.empty(); |
| 73 | + }).orElse(false); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Retrieves the attachment of wrapper of given key on the wrapper chain |
| 78 | + * by calling {@link Attachable#getAttachment(Object)}. |
| 79 | + * <p> |
| 80 | + * The wrapper chain consists of wrapper itself, followed by the wrappers |
| 81 | + * obtained by repeatedly calling {@link Wrapper#unwrap()}. |
| 82 | + * <p> |
| 83 | + * If the key exists in multiple wrappers, outer wrapper win. |
| 84 | + * |
| 85 | + * @param wrapper wrapper instance |
| 86 | + * @param key the attachment key |
| 87 | + * @param <W> the type of instances that be wrapped |
| 88 | + * @param <K> the type of attachment key |
| 89 | + * @param <V> the type of attachment value |
| 90 | + * @return the attachment value of wrapper of given key on the wrapper chain, |
| 91 | + * or null if the attachment is absent |
| 92 | + * @throws NullPointerException if any arguments is null or any wrapper {{@link Wrapper#unwrap()}} returns null |
| 93 | + * @throws ClassCastException if the return value is not type {@code <V>} |
| 94 | + * @see Attachable#getAttachment(Object) |
| 95 | + */ |
| 96 | + @Nullable |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + public static <W, K, V> V getAttachment(final W wrapper, final K key) { |
| 99 | + requireNonNull(wrapper, "wrapper is null"); |
| 100 | + requireNonNull(key, "key is null"); |
| 101 | + return travel(wrapper, w -> { |
| 102 | + if (w instanceof Attachable) { |
| 103 | + V value = ((Attachable<K, V>) w).getAttachment(key); |
| 104 | + return Optional.ofNullable(value); |
| 105 | + } else { |
| 106 | + return Optional.empty(); |
| 107 | + } |
| 108 | + }).orElse(null); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + /** |
| 113 | + * Traverses the wrapper chain, and apply the given {@code process} function to each wrapper, |
| 114 | + * and returns the first non-empty({@link Optional#empty()}) result of the process function, |
| 115 | + * otherwise returns {@link Optional#empty()}. |
| 116 | + * <p> |
| 117 | + * The wrapper chain consists of wrapper itself, followed by the wrappers |
| 118 | + * obtained by repeatedly calling {@link Wrapper#unwrap()}. |
| 119 | + * |
| 120 | + * @param wrapper wrapper instance |
| 121 | + * @param process process function |
| 122 | + * @param <W> the type of instances that be wrapped |
| 123 | + * @param <T> the return data type of process function |
| 124 | + * @return the first non-empty({@link Optional#empty()}) result of the process function, |
| 125 | + * otherwise returns {@link Optional#empty()}. |
| 126 | + * @throws NullPointerException if any arguments is null or any wrapper {{@link Wrapper#unwrap()}} returns null |
| 127 | + * @throws IllegalStateException if the adaptee of {@link WrapperAdapter} is a wrapper instance, |
| 128 | + * the use of WrapperAdapter is unnecessary! |
| 129 | + */ |
| 130 | + @NonNull |
| 131 | + @SuppressWarnings("unchecked") |
| 132 | + static <W, T> Optional<T> travel(final W wrapper, final Function<W, Optional<T>> process) { |
| 133 | + requireNonNull(wrapper, "wrapper is null"); |
| 134 | + requireNonNull(process, "process is null"); |
| 135 | + |
| 136 | + Object w = wrapper; |
| 137 | + while (true) { |
| 138 | + // process the instance on wrapper chain |
| 139 | + Optional<T> result = process.apply((W) w); |
| 140 | + if (result.isPresent()) return result; |
| 141 | + |
| 142 | + // also process the adaptee if it's a WrapperAdapter |
| 143 | + if (w instanceof WrapperAdapter) { |
| 144 | + final Object adaptee = ((WrapperAdapter<?>) w).adaptee(); |
| 145 | + if (adaptee instanceof Wrapper) { |
| 146 | + throw new IllegalStateException("adaptee(" + adaptee.getClass().getName() + ") of WrapperAdapter(" + w.getClass().getName() + ") is a wrapper instance, the use of WrapperAdapter is unnecessary!"); |
| 147 | + } |
| 148 | + |
| 149 | + Optional<T> r = process.apply((W) adaptee); |
| 150 | + if (r.isPresent()) return r; |
| 151 | + } |
| 152 | + |
| 153 | + if (!(w instanceof Wrapper)) return Optional.empty(); |
| 154 | + w = unwrapNonNull(w); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments