68fb22f3154e6bc08110dce60f467010b7033f29
[vfc/nfvo/wfengine.git] /
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 import org.eclipse.winery.common.ids.GenericId;
15
16 /**
17  * Holds a reference to a file "object" stored in the repository
18  * 
19  * Directories are NOT supported as we would have to reflect parent
20  * relationships there, too.
21  * 
22  * One has to create TOSCAelementId-objects for directories (e.g., scc-data)
23  */
24 public class RepositoryFileReference implements Comparable<RepositoryFileReference> {
25         
26         protected final GenericId parent;
27         protected final String fileName;
28         
29         
30         /**
31          * @param parent the id of the toscaElement the file is nested in
32          * @param fileName the file name. <em>Must not</em> contain any illegal
33          *            characters. java.nio.Path cannot be used as Path is tied to a
34          *            FileSystem
35          */
36         public RepositoryFileReference(GenericId parent, String fileName) {
37                 if (parent == null) {
38                         throw new IllegalArgumentException("Parent must not be null.");
39                 }
40                 if (fileName == null) {
41                         throw new IllegalArgumentException("Filename must not be null.");
42                 }
43                 this.parent = parent;
44                 this.fileName = fileName;
45         }
46         
47         public GenericId getParent() {
48                 return this.parent;
49         }
50         
51         public String getFileName() {
52                 return this.fileName;
53         }
54         
55         @Override
56         public boolean equals(Object obj) {
57                 if (obj instanceof RepositoryFileReference) {
58                         RepositoryFileReference otherRef = (RepositoryFileReference) obj;
59                         return (otherRef.fileName.equals(this.fileName)) && (otherRef.getParent().equals(this.getParent()));
60                 } else {
61                         return false;
62                 }
63         }
64         
65         @Override
66         public int hashCode() {
67                 return this.getParent().hashCode() ^ this.getFileName().hashCode();
68         }
69         
70         @Override
71         public int compareTo(RepositoryFileReference o) {
72                 int res;
73                 res = this.parent.compareTo(o.parent);
74                 if (res == 0) {
75                         res = this.fileName.compareTo(o.fileName);
76                 }
77                 return res;
78         }
79         
80         @Override
81         public String toString() {
82                 return this.getParent().toString() + "/" + this.getFileName();
83         }
84 }