BrightSide Workbench Full Report + Source Code
StreetItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.financials.cart.delivery;
20 
21 import java.io.Serializable;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import org.turro.string.Strings;
25 import org.turro.util.Range;
26 
31 public class StreetItem implements Serializable {
32 
33  private static final Pattern
34  PREFIX = Pattern.compile("([^\\(]+) \\(([^\\)]*)"),
35  RANGE = Pattern.compile("[^0-9]*([0-9]+)[^0-9]*");
36 
37  public static StreetItem parseLine(String line) {
38  String[] fields = line.split("\\s*,\\s*");
39  if(fields.length >= 5) {
40  // city, zipcode, street, even, odd
41  String street = fields[2];
42  Matcher matcher = PREFIX.matcher(street);
43  if(matcher.find()) {
44  street = matcher.group(2) + " " + matcher.group(1);
45  }
46  Range even = parseRange(fields[3]);
47  Range odd = parseRange(fields[4]);
48  return new StreetItem(fields[0], fields[1], street, even, odd);
49  }
50  return null;
51  }
52 
53  private static Range parseRange(String field) {
54  String[] numbers = field.split("\\s*\\-\\s*");
55  Integer start = 1, end = Integer.MAX_VALUE;
56  if(numbers.length == 2) {
57  Matcher matcher = RANGE.matcher(numbers[0]);
58  if(matcher.find()) {
59  start = Integer.valueOf(matcher.group(1));
60  }
61  matcher = RANGE.matcher(numbers[1]);
62  if(matcher.find()) {
63  end = Integer.valueOf(matcher.group(1));
64  }
65  }
66  return new Range<>(start, end);
67  }
68 
69  private final String city, zipcode, street;
70  private final Range<Integer> even, odd;
71 
72  public StreetItem(String city, String zipcode, String street, Range<Integer> even, Range<Integer> odd) {
73  this.city = city;
74  this.zipcode = zipcode;
75  this.street = street;
76  this.even = even;
77  this.odd = odd;
78  }
79 
80  public String getCity() {
81  return city;
82  }
83 
84  public String getZipcode() {
85  return zipcode;
86  }
87 
88  public String getStreet() {
89  return street;
90  }
91 
92  public Range<Integer> getEven() {
93  return even;
94  }
95 
96  public Range<Integer> getOdd() {
97  return odd;
98  }
99 
100  public boolean matches(String search) {
101  return street.matches(".*" + search + ".*");
102  }
103 
104  boolean isValid() {
105  return !Strings.isBlank(city) && !Strings.isBlank(street) &&
106  !Strings.isBlank(zipcode) && zipcode.matches("[0-9]{5}");
107  }
108 
109 }
StreetItem(String city, String zipcode, String street, Range< Integer > even, Range< Integer > odd)
Definition: StreetItem.java:72
static StreetItem parseLine(String line)
Definition: StreetItem.java:37