• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.examples.routeguide;
18 
19 import com.google.protobuf.util.JsonFormat;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.net.URL;
25 import java.nio.charset.Charset;
26 import java.util.List;
27 
28 /**
29  * Common utilities for the RouteGuide demo.
30  */
31 public class RouteGuideUtil {
32   private static final double COORD_FACTOR = 1e7;
33 
34   /**
35    * Gets the latitude for the given point.
36    */
getLatitude(Point location)37   public static double getLatitude(Point location) {
38     return location.getLatitude() / COORD_FACTOR;
39   }
40 
41   /**
42    * Gets the longitude for the given point.
43    */
getLongitude(Point location)44   public static double getLongitude(Point location) {
45     return location.getLongitude() / COORD_FACTOR;
46   }
47 
48   /**
49    * Gets the default features file from classpath.
50    */
getDefaultFeaturesFile()51   public static URL getDefaultFeaturesFile() {
52     return RouteGuideServer.class.getResource("route_guide_db.json");
53   }
54 
55   /**
56    * Parses the JSON input file containing the list of features.
57    */
parseFeatures(URL file)58   public static List<Feature> parseFeatures(URL file) throws IOException {
59     InputStream input = file.openStream();
60     try {
61       Reader reader = new InputStreamReader(input, Charset.forName("UTF-8"));
62       try {
63         FeatureDatabase.Builder database = FeatureDatabase.newBuilder();
64         JsonFormat.parser().merge(reader, database);
65         return database.getFeatureList();
66       } finally {
67         reader.close();
68       }
69     } finally {
70       input.close();
71     }
72   }
73 
74   /**
75    * Indicates whether the given feature exists (i.e. has a valid name).
76    */
exists(Feature feature)77   public static boolean exists(Feature feature) {
78     return feature != null && !feature.getName().isEmpty();
79   }
80 }
81