001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 2025 microBean™. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 011 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 012 * specific language governing permissions and limitations under the License. 013 */ 014package org.microbean.attributes; 015 016import java.lang.constant.ClassDesc; 017import java.lang.constant.DynamicConstantDesc; 018import java.lang.constant.MethodHandleDesc; 019import java.lang.constant.MethodTypeDesc; 020 021import java.util.Optional; 022 023import static java.lang.constant.ConstantDescs.BSM_INVOKE; 024import static java.lang.constant.ConstantDescs.CD_float; 025import static java.lang.constant.DirectMethodHandleDesc.Kind.STATIC; 026 027/** 028 * A {@link Value} whose value is a {@code float}. 029 * 030 * @param value the value 031 * 032 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a> 033 */ 034public final record FloatValue(float value) implements Value<FloatValue> { 035 036 @Override // Comparable<FloatValue> 037 public final int compareTo(final FloatValue other) { 038 if (other == null) { 039 return -1; 040 } else if (this.equals(other)) { 041 return 0; 042 } 043 return this.value() > other.value() ? 1 : -1; 044 } 045 046 @Override // Constable 047 public final Optional<DynamicConstantDesc<FloatValue>> describeConstable() { 048 final ClassDesc cd = ClassDesc.of(this.getClass().getName()); 049 return 050 Optional.of(DynamicConstantDesc.of(BSM_INVOKE, 051 MethodHandleDesc.ofMethod(STATIC, 052 cd, 053 "of", 054 MethodTypeDesc.of(cd, 055 CD_float)), 056 Float.valueOf(this.value()))); 057 } 058 059 @Override // Record 060 public final boolean equals(final Object other) { 061 return 062 other == this || 063 // Follow java.lang.annotation.Annotation requirements. 064 other != null && other.getClass() == this.getClass() && Float.valueOf(this.value()).equals(Float.valueOf(((FloatValue)other).value())); 065 } 066 067 @Override // Record 068 public final int hashCode() { 069 // Follow java.lang.annotation.Annotation requirements. 070 return Float.valueOf(this.value()).hashCode(); 071 } 072 073 @Override // Record 074 public final String toString() { 075 return String.valueOf(this.value()); 076 } 077 078 /** 079 * Returns a {@link FloatValue} suitable for the supplied arguments. 080 * 081 * @param f a {@code float} 082 * 083 * @return a non-{@code null} {@link FloatValue} 084 */ 085 public static final FloatValue of(final float f) { 086 return new FloatValue(f); 087 } 088 089}