Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.common / src / main / java / org / eclipse / winery / common / StringEncodedAndDecoded.java
1 /*******************************************************************************
2  * Copyright (c) 2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.common;
13
14
15
16 /**
17  * Meta class to handle things, where a String (URI, NCName, ...) may be
18  * URLencoded
19  */
20 public class StringEncodedAndDecoded implements Comparable<StringEncodedAndDecoded> {
21         
22         private String decoded = null;
23         private String encoded = null;
24         
25         
26         /**
27          * @param uri the URI to store
28          * @param URLencoded true iff the given URI is URLencoded
29          */
30         public StringEncodedAndDecoded(String uri, boolean URLencoded) {
31                 if (URLencoded) {
32                         this.encoded = uri;
33                 } else {
34                         this.decoded = uri;
35                 }
36         }
37         
38         public String getDecoded() {
39                 if (this.decoded == null) {
40                         this.decoded = Util.URLdecode(this.encoded);
41                 }
42                 return this.decoded;
43         }
44         
45         public String getEncoded() {
46                 if (this.encoded == null) {
47                         this.encoded = Util.URLencode(this.decoded);
48                 }
49                 return this.encoded;
50         }
51         
52         @Override
53         public int hashCode() {
54                 return this.getDecoded().hashCode();
55         }
56         
57         /**
58          * @return the URL path fragment to be used in an URL
59          */
60         public String getPathFragment() {
61                 return this.getEncoded();
62         }
63         
64         @Override
65         public String toString() {
66                 return this.getDecoded();
67         }
68         
69         @Override
70         public int compareTo(StringEncodedAndDecoded o) {
71                 return this.getDecoded().compareTo(o.getDecoded());
72         }
73         
74         /**
75          * Compares with the given object <br />
76          * Equality checking is made based on the decoded String
77          */
78         @Override
79         public boolean equals(Object o) {
80                 if (o instanceof String) {
81                         return this.getDecoded().equals(o);
82                 } else if (o instanceof StringEncodedAndDecoded) {
83                         return ((StringEncodedAndDecoded) o).getDecoded().equals(this.getDecoded());
84                 } else {
85                         return false;
86                 }
87         }
88 }