BrightSide Workbench Full Report + Source Code
StreetMapContext.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.IOException;
22 import java.nio.charset.Charset;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import java.util.stream.Collectors;
30 import org.turro.elephant.context.ElephantContext;
31 
37 
38  private final List<StreetItem> streets = new ArrayList<>();
39 
40  public StreetMapContext(String streetMapFile, long productId, long storeId, String logistic) {
41  super(productId, storeId, logistic);
42  loadFile(streetMapFile);
43  }
44 
45  @Override
46  public boolean match(String data) {
47  return false;
48  }
49 
50  public List<StreetItem> getStreets() {
51  return streets;
52  }
53 
54  public List<StreetItem> getStreets(String search) {
55  return streets.stream().filter(street -> street.matches(search)).collect(Collectors.toList());
56  }
57 
58  private void loadFile(String streetMapFile) {
59  try {
60  Files.lines(Path.of(ElephantContext.getRealPath(streetMapFile)),
61  Charset.forName("UTF-8")).forEach(line -> {
62  StreetItem street = StreetItem.parseLine(line);
63  if(street != null && street.isValid()) streets.add(street);
64  });
65  } catch (IOException ex) {
66  Logger.getLogger(StreetMapContext.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(streetMapFile), ex);
67  }
68  }
69 
70 }
static StreetItem parseLine(String line)
Definition: StreetItem.java:37
StreetMapContext(String streetMapFile, long productId, long storeId, String logistic)