github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/example/routeguide/java/RouteGuideUtil.java (about)

     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 example.routeguide.java;
    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 example.routeguide.Point;
    24  import example.routeguide.Feature;
    25  import example.routeguide.FeatureDatabase;
    26  
    27  import java.io.Reader;
    28  import java.net.URL;
    29  import java.nio.charset.Charset;
    30  import java.util.List;
    31  
    32  /**
    33   * Common utilities for the RouteGuide demo.
    34   */
    35  public class RouteGuideUtil {
    36    private static final double COORD_FACTOR = 1e7;
    37  
    38    /**
    39     * Gets the latitude for the given point.
    40     */
    41    public static double getLatitude(Point location) {
    42      return location.getLatitude() / COORD_FACTOR;
    43    }
    44  
    45    /**
    46     * Gets the longitude for the given point.
    47     */
    48    public static double getLongitude(Point location) {
    49      return location.getLongitude() / COORD_FACTOR;
    50    }
    51  
    52    /**
    53     * Gets the default features file from classpath.
    54     */
    55    public static URL getDefaultFeaturesFile() {
    56      String filename = "/example/routeguide/feature_db.json";
    57      URL r = RouteGuideUtil.class.getResource(filename);
    58      if (r == null) {
    59        throw new NullPointerException("No resource found at " + filename);
    60      }
    61      return r;
    62    }
    63  
    64    /**
    65     * Parses the JSON input file containing the list of features.
    66     */
    67    public static List<Feature> parseFeatures(URL file) throws IOException {
    68      System.out.println("Parsing feature db from " + file);
    69  
    70      InputStream input = file.openStream();
    71      try {
    72        Reader reader = new InputStreamReader(input, Charset.forName("UTF-8"));
    73        try {
    74          FeatureDatabase.Builder database = FeatureDatabase.newBuilder();
    75          JsonFormat.parser().merge(reader, database);
    76          return database.getFeatureList();
    77        } finally {
    78          reader.close();
    79        }
    80      } finally {
    81        input.close();
    82      }
    83    }
    84  
    85    /**
    86     * Indicates whether the given feature exists (i.e. has a valid name).
    87     */
    88    public static boolean exists(Feature feature) {
    89      return feature != null && !feature.getName().isEmpty();
    90    }
    91  }