View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.booter;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.Objects;
25  import java.util.Properties;
26  
27  import static java.nio.charset.StandardCharsets.ISO_8859_1;
28  import static org.apache.maven.surefire.api.util.ReflectionUtils.loadClass;
29  
30  /**
31   * @author Kristian Rosenvold
32   */
33  public final class TypeEncodedValue {
34      private final String type;
35      private final String value;
36  
37      public TypeEncodedValue(String type, String value) {
38          this.type = type;
39          this.value = value;
40      }
41  
42      private boolean isTypeClass() {
43          return Class.class.getName().equals(type);
44      }
45  
46      public Object getDecodedValue() {
47          return getDecodedValue(Thread.currentThread().getContextClassLoader());
48      }
49  
50      public Object getDecodedValue(ClassLoader classLoader) {
51          if (type.trim().isEmpty()) {
52              return null;
53          } else if (type.equals(String.class.getName())) {
54              return value;
55          } else if (isTypeClass()) {
56              return loadClass(classLoader, value);
57          } else if (type.equals(File.class.getName())) {
58              return new File(value);
59          } else if (type.equals(Boolean.class.getName())) {
60              return Boolean.valueOf(value);
61          } else if (type.equals(Integer.class.getName())) {
62              return Integer.valueOf(value);
63          } else if (type.equals(Properties.class.getName())) {
64              Properties result = new Properties();
65              try {
66                  result.load(new ByteArrayInputStream(value.getBytes(ISO_8859_1)));
67                  return result;
68              } catch (IOException e) {
69                  throw new IllegalStateException("bug in property conversion", e);
70              }
71          } else {
72              throw new IllegalArgumentException("Unknown parameter type: " + type);
73          }
74      }
75  
76      @Override
77      public boolean equals(Object o) {
78          if (this == o) {
79              return true;
80          }
81          if (o == null || getClass() != o.getClass()) {
82              return false;
83          }
84  
85          TypeEncodedValue that = (TypeEncodedValue) o;
86  
87          return equalsType(that) && equalsValue(that);
88      }
89  
90      @Override
91      public int hashCode() {
92          int result = type != null ? type.hashCode() : 0;
93          result = 31 * result + (value != null ? value.hashCode() : 0);
94          return result;
95      }
96  
97      @Override
98      public String toString() {
99          return "TypeEncodedValue{" + "type='" + type + '\'' + ", value='" + value + '\'' + '}';
100     }
101 
102     private boolean equalsType(TypeEncodedValue that) {
103         return Objects.equals(type, that.type);
104     }
105 
106     private boolean equalsValue(TypeEncodedValue that) {
107         return Objects.equals(value, that.value);
108     }
109 }