state) {
24 | switch (state.status) {
25 | case START:
26 | return Observable.empty();
27 | case PROGRESS:
28 | return Observable.empty();
29 | case SUCCESS:
30 | return Observable.just(state.action);
31 | case FAIL:
32 | return Observable.error(new JanetActionException(state.exception, state.action));
33 | default:
34 | throw new IllegalArgumentException("Action status is unknown");
35 | }
36 | }
37 | });
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/janet/src/main/java/io/techery/janet/helper/JanetActionException.java:
--------------------------------------------------------------------------------
1 | package io.techery.janet.helper;
2 |
3 | import io.techery.janet.JanetException;
4 |
5 | /**
6 | * JanetException with an action inside
7 | */
8 | public class JanetActionException extends JanetException {
9 |
10 | private final Object action;
11 |
12 | JanetActionException(Throwable cause, Object action) {
13 | super(cause);
14 | this.action = action;
15 | }
16 |
17 | public Object getAction() {
18 | return action;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/janet/src/main/java/io/techery/janet/internal/TypeToken.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.techery.janet.internal;
18 |
19 | import java.lang.reflect.Array;
20 | import java.lang.reflect.GenericArrayType;
21 | import java.lang.reflect.ParameterizedType;
22 | import java.lang.reflect.Type;
23 | import java.lang.reflect.TypeVariable;
24 | import java.util.HashMap;
25 | import java.util.Map;
26 |
27 | /**
28 | * Represents a generic type {@code T}.
29 | *
30 | * You can use this class to get the generic type for a class. For example,
31 | * to get the generic type for Collection<Foo>
, you can use:
32 | *
33 | * Type typeOfCollectionOfFoo = new TypeToken<Collection<Foo>>(){}.getType()
34 | *
35 | *
36 | *
Assumes {@code Type} implements {@code equals()} and {@code hashCode()}
37 | * as a value (as opposed to identity) comparison.
38 | *
39 | * Also implements {@link #isAssignableFrom(Type)} to check type-safe
40 | * assignability.
41 | *
42 | * @author Bob Lee
43 | * @author Sven Mawson
44 | */
45 | public abstract class TypeToken {
46 |
47 | final Class super T> rawType;
48 | final Type type;
49 |
50 | /**
51 | * Constructs a new type token. Derives represented class from type
52 | * parameter.
53 | *
54 | * Clients create an empty anonymous subclass. Doing so embeds the type
55 | * parameter in the anonymous class's type hierarchy so we can reconstitute
56 | * it at runtime despite erasure.
57 | *
58 | *
For example:
59 | *
60 | * {@literal TypeToken> t = new TypeToken>}(){}
61 | *
62 | */
63 | @SuppressWarnings("unchecked")
64 | protected TypeToken() {
65 | this.type = getSuperclassTypeParameter(getClass());
66 | this.rawType = (Class super T>) getRawType(type);
67 | }
68 |
69 | /**
70 | * Unsafe. Constructs a type token manually.
71 | */
72 | @SuppressWarnings({"unchecked"})
73 | private TypeToken(Type type) {
74 | this.rawType = (Class super T>) getRawType(nonNull(type, "type"));
75 | this.type = type;
76 | }
77 |
78 | private static T nonNull(T o, String message) {
79 | if (o == null) {
80 | throw new NullPointerException(message);
81 | }
82 | return o;
83 | }
84 |
85 | /**
86 | * Gets type from super class's type parameter.
87 | */
88 | static Type getSuperclassTypeParameter(Class> subclass) {
89 | Type superclass = subclass.getGenericSuperclass();
90 | if (superclass instanceof Class) {
91 | throw new RuntimeException("Missing type parameter.");
92 | }
93 | return ((ParameterizedType) superclass).getActualTypeArguments()[0];
94 | }
95 |
96 | /**
97 | * Gets type token from super class's type parameter.
98 | */
99 | static TypeToken> fromSuperclassTypeParameter(Class> subclass) {
100 | return new SimpleTypeToken