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_Integer; 025import static java.lang.constant.ConstantDescs.CD_short; 026import static java.lang.constant.DirectMethodHandleDesc.Kind.STATIC; 027import static java.lang.constant.DirectMethodHandleDesc.Kind.VIRTUAL; 028 029/** 030 * A {@link Value} whose value is a {@code short}. 031 * 032 * @param value the value 033 * 034 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a> 035 */ 036public final record ShortValue(short value) implements Value<ShortValue> { 037 038 @Override // Comparable<ShortValue> 039 public final int compareTo(final ShortValue other) { 040 if (other == null) { 041 return -1; 042 } else if (this.equals(other)) { 043 return 0; 044 } 045 return this.value() > other.value() ? 1 : -1; 046 } 047 048 @Override // Constable 049 public final Optional<DynamicConstantDesc<ShortValue>> describeConstable() { 050 final ClassDesc cd = ClassDesc.of(this.getClass().getName()); 051 return 052 Optional.of(DynamicConstantDesc.of(BSM_INVOKE, 053 MethodHandleDesc.ofMethod(STATIC, 054 cd, 055 "of", 056 MethodTypeDesc.of(cd, 057 CD_short)), 058 DynamicConstantDesc.of(BSM_INVOKE, 059 MethodHandleDesc.ofMethod(VIRTUAL, 060 CD_Integer, 061 "shortValue", 062 MethodTypeDesc.of(CD_short)), 063 Integer.valueOf(this.value())))); 064 } 065 066 @Override // Record 067 public final boolean equals(final Object other) { 068 return 069 other == this || 070 // Follow java.lang.annotation.Annotation requirements. 071 other != null && other.getClass() == this.getClass() && this.value() == ((ShortValue)other).value(); 072 } 073 074 @Override // Record 075 public final int hashCode() { 076 // Follow java.lang.annotation.Annotation requirements. 077 return Short.valueOf(this.value()).hashCode(); 078 } 079 080 @Override // Record 081 public final String toString() { 082 return String.valueOf(this.value()); 083 } 084 085 /** 086 * Returns a {@link ShortValue} suitable for the supplied arguments. 087 * 088 * @param s a {@code short} 089 * 090 * @return a non-{@code null} {@link ShortValue} 091 */ 092 public static final ShortValue of(final short s) { 093 return new ShortValue(s); 094 } 095 096}