View Javadoc

1   /*
2    * Copyright (C) 2012 Atlassian
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 com.atlassian.jira.rest.client.api.domain.input;
18  
19  import com.google.common.base.Function;
20  import com.google.common.collect.ImmutableList;
21  import com.google.common.collect.Iterables;
22  import com.google.common.collect.Lists;
23  
24  import javax.annotation.Nullable;
25  import java.util.List;
26  
27  /**
28   * This class allows to register {@link ValueTransformer} objects and then perform value transformation using
29   * registered transformers by invoking {@link ValueTransformerManager#apply(Object)}.
30   *
31   * @since v1.0
32   */
33  public class ValueTransformerManager implements Function<Object, Object> {
34      public final List<ValueTransformer> valueTransformers = Lists.newArrayList();
35  
36      public ValueTransformerManager() {
37      }
38  
39      /**
40       * Registers new transformer at the end of list so it will be processed after existing transformers.
41       *
42       * @param transformer Transformer to register
43       * @return this
44       */
45      public ValueTransformerManager registerTransformer(final ValueTransformer transformer) {
46          valueTransformers.add(transformer);
47          return this;
48      }
49  
50      /**
51       * Registers new transformer at the beginning of list so it will be processed before existing transformers.
52       *
53       * @param transformer Transformer to register
54       * @return this
55       */
56      @SuppressWarnings("unused")
57      public ValueTransformerManager registerTransformerAsFirst(final ValueTransformer transformer) {
58          valueTransformers.add(0, transformer);
59          return this;
60      }
61  
62      /**
63       * Use registered transformers to transform given value.
64       *
65       * @param rawInput Value to transform
66       * @return transformed value
67       * @throws CannotTransformValueException when any of available transformers was able to transform given value
68       */
69      public Object apply(@Nullable Object rawInput) {
70          if (rawInput instanceof Iterable) {
71              @SuppressWarnings("unchecked") final Iterable<Object> rawInputObjects = (Iterable<Object>) rawInput;
72              return ImmutableList.copyOf(Iterables.transform(rawInputObjects, this));
73          }
74  
75          for (ValueTransformer valueTransformer : valueTransformers) {
76              final Object transformedValue = valueTransformer.apply(rawInput);
77              if (!ValueTransformer.CANNOT_HANDLE.equals(transformedValue)) {
78                  return transformedValue;
79              }
80          }
81  
82          throw new CannotTransformValueException("Any of available transformers was able to transform given value. Value is: "
83                  + (rawInput == null ? "NULL" : rawInput.getClass().getName() + ": " + rawInput.toString()));
84      }
85  }