001/* 002 * This file is part of OCaml-Java runtime. 003 * Copyright (C) 2007-2013 Xavier Clerc. 004 * 005 * OCaml-Java runtime is free software; you can redistribute it and/or modify 006 * it under the terms of the GNU Lesser General Public License as published by 007 * the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * OCaml-Java runtime is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.ocamljava.runtime.wrappers; 020 021import org.ocamljava.runtime.values.Value; 022 023/** 024 * The {@code OCamlChar} class is the wrapper class for OCaml values of 025 * type {@code char}. 026 * 027 * @author <a href="mailto:xclerc@ocamljava.org">Xavier Clerc</a> 028 * @version 2.0 029 * @since 2.0 030 */ 031public final class OCamlChar extends OCamlValue { 032 033 /** Wrapper for {@code OCamlChar} values. */ 034 public static final Wrapper<OCamlChar> WRAPPER = new SimpleWrapper<OCamlChar>() { 035 036 /** 037 * {@inheritDoc} 038 */ 039 @Override 040 public OCamlChar wrap(final Value v) { 041 return new OCamlChar(v); 042 } // end method 'wrap(Value)' 043 044 }; // end anonymous inner-class 045 046 /** 047 * Constructs a new instance wrapping the passed value. 048 * @param v value to wrap - should not be {@code null} 049 */ 050 private OCamlChar(final Value v) { 051 super(v); 052 } // end constructor(Value) 053 054 /** 055 * {@inheritDoc} 056 */ 057 @Override 058 public Wrapper<? extends OCamlChar> getWrapper() { 059 return OCamlChar.WRAPPER; 060 } // end method 'getWrapper()' 061 062 /** 063 * {@inheritDoc} 064 */ 065 @Override 066 public Wrapper<? extends OCamlValue> getWrapper(final int idx) { 067 return OCamlUnit.WRAPPER; 068 } // end method 'getWrapper(int)' 069 070 /** 071 * Returns the wrapped value as a {@code byte}. 072 * @return the wrapped value, converted to a {@code byte} 073 */ 074 public byte byteValue() { 075 return (byte) this.value.asLong(); 076 } // end method 'byteValue()' 077 078 /** 079 * Returns the wrapped value as a {@code char}. 080 * @return the wrapped value, converted to a {@code char} 081 */ 082 public char charValue() { 083 return (char) this.value.asLong(); 084 } // end method 'charValue()' 085 086 /** 087 * Returns the wrapped value as a {@code int}. 088 * @return the wrapped value, converted to a {@code int} 089 */ 090 public int intValue() { 091 return (int) this.value.asLong(); 092 } // end method 'intValue()' 093 094 /** 095 * {@inheritDoc} 096 */ 097 @Override 098 public int hashCode() { 099 return (int) this.value.asLong(); 100 } // end method 'hashCode()' 101 102 /** 103 * {@inheritDoc} 104 */ 105 @Override 106 public boolean equals(final Object obj) { 107 if (obj instanceof OCamlChar) { 108 final OCamlChar that = (OCamlChar) obj; 109 return this.value.asLong() == that.value.asLong(); 110 } else { 111 return false; 112 } // end if/else 113 } // end method 'equals(Object)' 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override 119 public String toString() { 120 final StringBuilder sb = new StringBuilder(); 121 sb.append("OCamlChar("); 122 sb.append(this.value.asLong()); 123 sb.append(")"); 124 return sb.toString(); 125 } // end method 'toString()' 126 127 /** 128 * Constructs a new {@code int} value, and wraps it. 129 * @param v value to wrap 130 * @return a new {@code OCamlChar} instance wrapping the passed value 131 */ 132 public static OCamlChar create(final long v) { 133 return new OCamlChar(Value.createLong(v)); 134 } // end method 'create(long)' 135 136 /** 137 * Wraps the passed value. 138 * @param v value to wrap - should not be {@code null} 139 * @return a new {@code OCamlChar} instance wrapping the passed value 140 */ 141 public static OCamlChar wrap(final Value v) { 142 assert v != null : "null v"; 143 return new OCamlChar(v); 144 } // end method 'wrap(Value)' 145 146 /** 147 * Returns a wrapper for {@code OCamlChar} values. 148 * @return a wrapper for {@code OCamlChar} values 149 */ 150 public static Wrapper<? extends OCamlChar> wrapper() { 151 return OCamlChar.WRAPPER; 152 } // end method 'wrapper()' 153 154} // end class 'OCamlChar'