View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.project.persistence.utils;
21  
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  /**
26   * Class representing one section of a bundle path.
27   */
28  final class PathSection {
29      private static final Pattern pattern = Pattern
30              .compile("([^/\\[\\]]+)\\[(\\d+|-1)\\]");
31      private final String path;
32      private final int index;
33  
34      private PathSection(String path, int index) {
35          this.path = path;
36          this.index = index;
37      }
38  
39      public String getPath() {
40          return path;
41      }
42  
43      public int getIndex() {
44          return index;
45      }
46      
47      public static PathSection fromString(String pt) {
48          Matcher matcher = pattern.matcher(pt);
49          if (!matcher.matches()) {
50              throw new IllegalArgumentException(
51                      String.format(
52                              "Bad path section for nested bundle update: '%s'. "
53                                      + "Non-terminal paths should contain relation name and index.",
54                              pt));
55          }
56          return new PathSection(matcher.group(1), Integer.parseInt(
57                  matcher.group(2)));
58      }
59      
60      @Override
61      public int hashCode() {
62          return index * 31 + path.hashCode();
63      }
64      
65      @Override
66      public boolean equals(Object other) {
67          if (this == other)
68              return true;
69          else if (!(other instanceof PathSection))
70              return false;
71          else {
72              PathSection that = (PathSection)other;
73              return path.equals(that.path) && index == that.index;
74          }
75      }
76      
77  
78      public String toString() {
79          return path + "[" + index + "]";
80      }    
81  }