浏览代码

convert some H3 classes to records (#113574)

Ignacio Vera 1 年之前
父节点
当前提交
71c252c274

+ 12 - 31
libs/h3/src/main/java/org/elasticsearch/h3/BaseCells.java

@@ -27,27 +27,14 @@ package org.elasticsearch.h3;
  */
  */
 final class BaseCells {
 final class BaseCells {
 
 
-    private static class BaseCellData {
-        // "home" face and normalized ijk coordinates on that face
-        final int homeFace;
-        final int homeI;
-        final int homeJ;
-        final int homeK;
-        // is this base cell a pentagon?
-        final boolean isPentagon;
-        // if a pentagon, what are its two clockwise offset
-        final int[] cwOffsetPent;
-
-        /// faces?
-        BaseCellData(int homeFace, int homeI, int homeJ, int homeK, boolean isPentagon, int[] cwOffsetPent) {
-            this.homeFace = homeFace;
-            this.homeI = homeI;
-            this.homeJ = homeJ;
-            this.homeK = homeK;
-            this.isPentagon = isPentagon;
-            this.cwOffsetPent = cwOffsetPent;
-        }
-    }
+    private record BaseCellData(
+        int homeFace, // "home" face and normalized ijk coordinates on that face
+        int homeI,
+        int homeJ,
+        int homeK,
+        boolean isPentagon,   // is this base cell a pentagon?
+        int[] cwOffsetPent  // if a pentagon, what are its two clockwise offset
+    ) {}
 
 
     /**
     /**
      * Resolution 0 base cell data table.
      * Resolution 0 base cell data table.
@@ -185,16 +172,10 @@ final class BaseCells {
     /**
     /**
      *  base cell at a given ijk and required rotations into its system
      *  base cell at a given ijk and required rotations into its system
      */
      */
-    private static class BaseCellRotation {
-        final int baseCell;  // base cell number
-        final int ccwRot60;  // number of ccw 60 degree rotations relative to current
-        /// face
-
-        BaseCellRotation(int baseCell, int ccwRot60) {
-            this.baseCell = baseCell;
-            this.ccwRot60 = ccwRot60;
-        }
-    }
+    record BaseCellRotation(
+        int baseCell, // base cell number
+        int ccwRot60  // number of ccw 60 degree rotations relative to current
+    ) {}
 
 
     /** @brief Resolution 0 base cell lookup table for each face.
     /** @brief Resolution 0 base cell lookup table for each face.
      *
      *

+ 7 - 19
libs/h3/src/main/java/org/elasticsearch/h3/FaceIJK.java

@@ -149,25 +149,13 @@ final class FaceIJK {
     /**
     /**
      *  Information to transform into an adjacent face IJK system
      *  Information to transform into an adjacent face IJK system
      */
      */
-    private static class FaceOrientIJK {
-        // face number
-        final int face;
-        // res 0 translation relative to primary face
-        final int translateI;
-        final int translateJ;
-        final int translateK;
-        // number of 60 degree ccw rotations relative to primary
-        final int ccwRot60;
-
-        // face
-        FaceOrientIJK(int face, int translateI, int translateJ, int translateK, int ccwRot60) {
-            this.face = face;
-            this.translateI = translateI;
-            this.translateJ = translateJ;
-            this.translateK = translateK;
-            this.ccwRot60 = ccwRot60;
-        }
-    }
+    private record FaceOrientIJK(
+        int face, // face number
+        int translateI, // res 0 translation relative to primary face
+        int translateJ,
+        int translateK,
+        int ccwRot60// number of 60 degree ccw rotations relative to primary
+    ) {}
 
 
     /**
     /**
      *  Definition of which faces neighbor each other.
      *  Definition of which faces neighbor each other.

+ 3 - 1
libs/h3/src/main/java/org/elasticsearch/h3/H3Index.java

@@ -325,7 +325,9 @@ final class H3Index {
                 foundFirstNonZeroDigit = true;
                 foundFirstNonZeroDigit = true;
 
 
                 // adjust for deleted k-axes sequence
                 // adjust for deleted k-axes sequence
-                if (h3LeadingNonZeroDigit(h) == CoordIJK.Direction.K_AXES_DIGIT.digit()) h = h3Rotate60ccw(h);
+                if (h3LeadingNonZeroDigit(h) == CoordIJK.Direction.K_AXES_DIGIT.digit()) {
+                    h = h3Rotate60ccw(h);
+                }
             }
             }
         }
         }
         return h;
         return h;

+ 0 - 5
libs/h3/src/main/java/org/elasticsearch/h3/HexRing.java

@@ -290,11 +290,6 @@ final class HexRing {
         { 0, 0, 1, 0, 1, 5, 1 },   // base cell 121
         { 0, 0, 1, 0, 1, 5, 1 },   // base cell 121
     };
     };
 
 
-    private static final int E_SUCCESS = 0; // Success (no error)
-    private static final int E_PENTAGON = 9;  // Pentagon distortion was encountered which the algorithm
-    private static final int E_CELL_INVALID = 5; // `H3Index` cell argument was not valid
-    private static final int E_FAILED = 1;  // The operation failed but a more specific error is not available
-
     /**
     /**
      * Directions used for traversing a hexagonal ring counterclockwise around
      * Directions used for traversing a hexagonal ring counterclockwise around
      * {1, 0, 0}
      * {1, 0, 0}

+ 4 - 9
libs/h3/src/main/java/org/elasticsearch/h3/Vec2d.java

@@ -27,7 +27,10 @@ import java.util.Objects;
 /**
 /**
  *  2D floating-point vector
  *  2D floating-point vector
  */
  */
-final class Vec2d {
+record Vec2d(
+    double x, // x component
+    double y  // y component
+) {
 
 
     /** 1/sin(60') **/
     /** 1/sin(60') **/
     private static final double M_RSIN60 = 1.0 / Constants.M_SQRT3_2;
     private static final double M_RSIN60 = 1.0 / Constants.M_SQRT3_2;
@@ -90,14 +93,6 @@ final class Vec2d {
         { 2.361378999196363184, 0.266983896803167583, 4.455774101589558636 },  // face 19
         { 2.361378999196363184, 0.266983896803167583, 4.455774101589558636 },  // face 19
     };
     };
 
 
-    private final double x;  /// < x component
-    private final double y;  /// < y component
-
-    Vec2d(double x, double y) {
-        this.x = x;
-        this.y = y;
-    }
-
     /**
     /**
      * Determines the center point in spherical coordinates of a cell given by this 2D
      * Determines the center point in spherical coordinates of a cell given by this 2D
      * hex coordinates on a particular icosahedral face.
      * hex coordinates on a particular icosahedral face.

+ 1 - 10
libs/h3/src/main/java/org/elasticsearch/h3/Vec3d.java

@@ -26,7 +26,7 @@ package org.elasticsearch.h3;
 /**
 /**
  *  3D floating-point vector
  *  3D floating-point vector
  */
  */
-final class Vec3d {
+record Vec3d(double x, double y, double z) {
 
 
     /** icosahedron face centers in x/y/z on the unit sphere */
     /** icosahedron face centers in x/y/z on the unit sphere */
     public static final Vec3d[] faceCenterPoint = new Vec3d[] {
     public static final Vec3d[] faceCenterPoint = new Vec3d[] {
@@ -52,14 +52,6 @@ final class Vec3d {
         new Vec3d(-0.1092625278784796, 0.4811951572873210, -0.8697775121287253)   // face 19
         new Vec3d(-0.1092625278784796, 0.4811951572873210, -0.8697775121287253)   // face 19
     };
     };
 
 
-    private final double x, y, z;
-
-    private Vec3d(double x, double y, double z) {
-        this.x = x;
-        this.y = y;
-        this.z = z;
-    }
-
     /**
     /**
      * Calculate the square of the distance between two 3D coordinates.
      * Calculate the square of the distance between two 3D coordinates.
      *
      *
@@ -238,5 +230,4 @@ final class Vec3d {
     private static double magnitude(double x, double y, double z) {
     private static double magnitude(double x, double y, double z) {
         return Math.sqrt(square(x) + square(y) + square(z));
         return Math.sqrt(square(x) + square(y) + square(z));
     }
     }
-
 }
 }