java

package
v0.58.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package java provides utilities for transliterating Java code to Go. It includes functions that emulate Java-specific behaviour where Go differs, and helpers for implementing Java's class-based polymorphism.

Index

Constants

This section is empty.

Variables

View Source
var CanonicalNaN = math.Float64frombits(0x7FF8000000000000)

CanonicalNaN is Java's canonical NaN value (Double.NaN). Go's math.NaN() may produce a different NaN bit pattern (0x7FF8000000000001) than Java's canonical NaN (0x7FF8000000000000). This difference matters for binary formats like WKB where byte-for-byte compatibility with Java is needed.

Functions

func AbsInt

func AbsInt(x int) int

AbsInt implements Java's Math.abs(int) for integers. Go's math.Abs() only works on float64, so this provides the integer version.

func Cast

func Cast[T Polymorphic](obj Polymorphic) T

Cast extracts type T from obj's type hierarchy, panicking if the cast fails. This is equivalent to Java's cast operator: (T) obj

Panics with a descriptive message if obj cannot be cast to T (equivalent to Java's ClassCastException).

func InstanceOf

func InstanceOf[T any](obj Polymorphic) bool

InstanceOf checks if obj's type hierarchy includes T. This is equivalent to Java's instanceof operator for polymorphic types that use the child-chain dispatch pattern.

The function checks: 1. If obj itself is of type T 2. If any parent type (via GetParent chain) is of type T 3. If any child type (via GetChild chain) is of type T

This correctly handles inheritance hierarchies:

// Given a LinearRing (which extends LineString which extends Geometry)
InstanceOf[*Geom_Geometry](ring)    // true
InstanceOf[*Geom_LineString](ring)  // true
InstanceOf[*Geom_LinearRing](ring)  // true
InstanceOf[*Geom_Polygon](ring)     // false

Returns false if obj is nil.

func Round

func Round(val float64) float64

Round implements Java's Math.round() semantics: rounds to the nearest integer with ties going towards positive infinity. This differs from Go's math.Round() which rounds ties away from zero.

Examples:

Round(1.5)    // 2 (same as Go)
Round(-1.5)   // -1 (Go returns -2)
Round(-1232.5) // -1232 (Go returns -1233)

func SortedKeysString

func SortedKeysString[V any](m map[string]V) []string

SortedKeysString returns the keys of a map[string]V in sorted order. This is used when transliterating Java code that iterates over maps, because Go's map iteration order is randomized while Java's HashMap iteration order is consistent (even though unspecified), and Java's TreeMap iteration order is sorted.

Types

type Polymorphic

type Polymorphic interface {
	GetChild() Polymorphic
	GetParent() Polymorphic
}

func GetLeaf

func GetLeaf(obj Polymorphic) Polymorphic

GetLeaf walks the child chain to find the leaf (concrete) type. This is used by dispatchers to find the most-derived implementation of a method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL