-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from cquiroz/format
Format implementations
- Loading branch information
Showing
10 changed files
with
376 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
core/src/main/scala/java/text/AttributedCharacterIterator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package java.text | ||
|
||
trait AttributedCharacterIterator extends CharacterIterator { | ||
def getRunStart(): Int | ||
|
||
def getRunStart(attribute: AttributedCharacterIterator.Attribute): Int | ||
|
||
def getRunStart(attributes: java.util.Set[_ <: AttributedCharacterIterator.Attribute]): Int | ||
|
||
def getRunLimit(): Int | ||
|
||
def getRunLimit(attribute: AttributedCharacterIterator.Attribute): Int | ||
|
||
def getRunLimit(attributes: java.util.Set[_ <: AttributedCharacterIterator.Attribute]): Int | ||
|
||
def getAttributes(): java.util.Map[AttributedCharacterIterator.Attribute, AnyRef] | ||
|
||
def getAttribute(attribute: AttributedCharacterIterator.Attribute): AnyRef | ||
|
||
def getAllAttributeKeys(): java.util.Set[AttributedCharacterIterator.Attribute] | ||
} | ||
|
||
object AttributedCharacterIterator { | ||
class Attribute protected (private[this] val name: String) { | ||
override final def equals(that: Any): Boolean = that match { | ||
case t: Attribute => this.eq(t) // As per javadocs | ||
case _ => false | ||
} | ||
|
||
override final def hashCode(): Int = name.hashCode | ||
|
||
override def toString: String = s"java.text.AttributedCharacterIterator$$Attribute($getName)" | ||
|
||
protected def getName(): String = name | ||
} | ||
|
||
object Attribute { | ||
val LANGUAGE: Attribute = new Attribute("language") | ||
val READING: Attribute = new Attribute("reading") | ||
val INPUT_METHOD_SEGMENT: Attribute = new Attribute("input_method_segment") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package java.text | ||
|
||
trait CharacterIterator extends Cloneable { | ||
def first(): Char | ||
|
||
def last(): Char | ||
|
||
def current(): Char | ||
|
||
def next(): Char | ||
|
||
def previous(): Char | ||
|
||
def setIndex(position: Int): Char | ||
|
||
def getBeginIndex(): Int | ||
|
||
def getEndIndex(): Int | ||
|
||
def getIndex(): Int | ||
} | ||
|
||
object CharacterIterator { | ||
val DONE: Char = '\uFFFF' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package java.text | ||
|
||
class FieldPosition(private[this] val attribute: Format.Field, private[this] val fieldId: Int) { | ||
private[this] var beginIndex: Int = 0 | ||
private[this] var endIndex: Int = 0 | ||
|
||
def this(attribute: Format.Field) = this(attribute, -1) | ||
|
||
def this(fieldId: Int) = this(null, fieldId) | ||
|
||
def getFieldAttribute(): Format.Field = attribute | ||
|
||
def getField(): Int = fieldId | ||
|
||
def getBeginIndex(): Int = beginIndex | ||
|
||
def getEndIndex(): Int = endIndex | ||
|
||
def setBeginIndex(bi: Int): Unit = beginIndex = bi | ||
|
||
def setEndIndex(ei: Int): Unit = endIndex = ei | ||
|
||
override def equals(other: Any): Boolean = other match { | ||
case that: FieldPosition => | ||
getBeginIndex == that.getBeginIndex && | ||
getEndIndex == that.getEndIndex && | ||
getFieldAttribute == that.getFieldAttribute && | ||
getField == that.getField | ||
case _ => false | ||
} | ||
|
||
override def hashCode(): Int = { | ||
// NOTE, the JVM doesn't use field attribute on hash but it uses on equal | ||
val state = Seq(getBeginIndex, getEndIndex,/* getFieldAttribute,*/ getField) | ||
state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b) | ||
} | ||
|
||
|
||
override def toString(): String = | ||
s"java.text.FieldPosition[field=$getField,attribute=$getFieldAttribute,beginIndex=$getBeginIndex,endIndex=$getEndIndex]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package java.text | ||
|
||
import AttributedCharacterIterator.Attribute | ||
|
||
abstract class Format protected () extends Cloneable { | ||
def format(obj: AnyRef): String = format(obj, new StringBuffer(), new FieldPosition(0)).toString() | ||
|
||
def format(obj: AnyRef, toAppendTo: StringBuffer, pos: FieldPosition): StringBuffer | ||
|
||
def formatToCharacterIterator(obj: AnyRef): AttributedCharacterIterator = | ||
new Format.EmptyAttributedCharacterIterator | ||
|
||
def parseObject(source: String, pos: ParsePosition): AnyRef | ||
|
||
def parseObject(source: String): AnyRef = | ||
parseObject(source, new ParsePosition(0)) | ||
} | ||
|
||
object Format { | ||
private class EmptyAttributedCharacterIterator extends AttributedCharacterIterator { | ||
override def getAttributes: java.util.Map[Attribute, AnyRef] = | ||
new java.util.HashMap() | ||
|
||
override def getAttribute(attribute: Attribute): AnyRef = null | ||
|
||
override def getAllAttributeKeys: java.util.Set[Attribute] = | ||
new java.util.TreeSet() | ||
|
||
override def getRunLimit: Int = 0 | ||
|
||
override def getRunLimit(attribute: Attribute): Int = 0 | ||
|
||
override def getRunLimit(attributes: java.util.Set[_ <: Attribute]): Int = 0 | ||
|
||
override def getRunStart: Int = 0 | ||
|
||
override def getRunStart(attribute: Attribute): Int = 0 | ||
|
||
override def getRunStart(attributes: java.util.Set[_ <: Attribute]): Int = 0 | ||
|
||
override def next(): Char = 0 | ||
|
||
override def setIndex(position: Int): Char = 0 | ||
|
||
override def getIndex: Int = 0 | ||
|
||
override def last(): Char = 0 | ||
|
||
override def getBeginIndex: Int = 0 | ||
|
||
override def getEndIndex: Int = 0 | ||
|
||
override def current(): Char = 0 | ||
|
||
override def previous(): Char = 0 | ||
|
||
override def first(): Char = 0 | ||
} | ||
|
||
class Field protected (private[this] val name: String) extends AttributedCharacterIterator.Attribute(name) { | ||
|
||
override def toString(): String = s"${getClass.getName}($name)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package java.text | ||
|
||
class ParseException(private[this] val s: String, private[this] val errorOffset: Int) | ||
extends Exception(s) { | ||
|
||
def getErrorOffset(): Int = errorOffset | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package java.text | ||
|
||
class ParsePosition(private[this] var index: Int) { | ||
private[this] var errorIndex: Int = -1 | ||
|
||
def getIndex(): Int = index | ||
|
||
def setIndex(index: Int): Unit = this.index = index | ||
|
||
def setErrorIndex(ei: Int): Unit = this.errorIndex = ei | ||
|
||
def getErrorIndex(): Int = errorIndex | ||
|
||
override def equals(other: Any): Boolean = other match { | ||
case that: ParsePosition => | ||
getErrorIndex == that.getErrorIndex && | ||
getIndex == that.getIndex | ||
case _ => false | ||
} | ||
|
||
override def hashCode(): Int = { | ||
val state = Seq(getErrorIndex, getIndex) | ||
state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b) | ||
} | ||
|
||
override def toString(): String = | ||
s"java.text.ParsePosition[index=$getIndex,errorIndex=$getErrorIndex]" | ||
} |
19 changes: 19 additions & 0 deletions
19
testSuite/shared/src/test/scala/testsuite/javalib/text/AttributedCharacterIteratorTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package testsuite.javalib.text | ||
|
||
import java.text.AttributedCharacterIterator.Attribute | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
class AttributedCharacterIteratorTest { | ||
@Test def test_static_value_to_string(): Unit = { | ||
assertEquals("java.text.AttributedCharacterIterator$Attribute(language)", Attribute.LANGUAGE.toString) | ||
assertEquals("java.text.AttributedCharacterIterator$Attribute(reading)", Attribute.READING.toString) | ||
assertEquals("java.text.AttributedCharacterIterator$Attribute(input_method_segment)", Attribute.INPUT_METHOD_SEGMENT.toString) | ||
} | ||
|
||
@Test def test_equals(): Unit = { | ||
assertEquals(Attribute.LANGUAGE, Attribute.LANGUAGE) | ||
assertFalse(Attribute.READING == Attribute.LANGUAGE) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
testSuite/shared/src/test/scala/testsuite/javalib/text/CharacterIteratorTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package testsuite.javalib.text | ||
|
||
import java.text.CharacterIterator | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
class CharacterIteratorTest { | ||
@Test def test_done(): Unit = { | ||
assertEquals('\uFFFF', CharacterIterator.DONE) | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
testSuite/shared/src/test/scala/testsuite/javalib/text/FieldPositionTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package testsuite.javalib.text | ||
|
||
import java.text.{FieldPosition, Format} | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
class FieldPositionTest { | ||
case class TestField(name: String) extends Format.Field(name) | ||
|
||
@Test def test_constructors(): Unit = { | ||
val field = TestField("abc") | ||
|
||
val f1 = new FieldPosition(field, 1) | ||
assertEquals(field, f1.getFieldAttribute()) | ||
assertEquals(1, f1.getField()) | ||
assertEquals(0, f1.getBeginIndex()) | ||
assertEquals(0, f1.getEndIndex()) | ||
|
||
val f2 = new FieldPosition(field) | ||
assertEquals(field, f2.getFieldAttribute()) | ||
assertEquals(-1, f2.getField()) | ||
assertEquals(0, f2.getBeginIndex()) | ||
assertEquals(0, f2.getEndIndex()) | ||
|
||
val f3 = new FieldPosition(1) | ||
assertNull(f3.getFieldAttribute()) | ||
assertEquals(1, f3.getField()) | ||
assertEquals(0, f3.getBeginIndex()) | ||
assertEquals(0, f3.getEndIndex()) | ||
} | ||
|
||
@Test def test_begin_end_index(): Unit = { | ||
val field = TestField("abc") | ||
|
||
val f1 = new FieldPosition(field, 1) | ||
f1.setBeginIndex(10) | ||
assertEquals(10, f1.getBeginIndex()) | ||
f1.setBeginIndex(-10) | ||
assertEquals(-10, f1.getBeginIndex()) | ||
|
||
f1.setEndIndex(10) | ||
assertEquals(10, f1.getEndIndex()) | ||
f1.setEndIndex(-10) | ||
assertEquals(-10, f1.getEndIndex()) | ||
} | ||
|
||
@Test def test_equals_hash_code(): Unit = { | ||
val field = TestField("abc") | ||
|
||
val f1 = new FieldPosition(field, 1) | ||
assertEquals(f1, f1) | ||
assertEquals(f1.hashCode(), f1.hashCode()) | ||
|
||
val f2 = new FieldPosition(field, 1) | ||
assertEquals(f1, f2) | ||
assertEquals(f1.hashCode(), f2.hashCode()) | ||
|
||
val f3 = new FieldPosition(field) | ||
assertFalse(f1.equals(f3)) | ||
assertFalse(f1.hashCode() == f3.hashCode()) | ||
|
||
val f4 = new FieldPosition(1) | ||
assertFalse(f1.equals(f4)) | ||
// hashcode is broken on the JVM | ||
assertEquals(f1.hashCode(), f4.hashCode()) | ||
|
||
f2.setBeginIndex(1) | ||
assertFalse(f1.equals(f2)) | ||
assertFalse(f1.hashCode() == f2.hashCode()) | ||
|
||
val f5 = new FieldPosition(field, 1) | ||
f5.setEndIndex(1) | ||
assertFalse(f1.equals(f5)) | ||
assertFalse(f1.hashCode() == f5.hashCode()) | ||
} | ||
|
||
@Test def test_to_string(): Unit = { | ||
val field = TestField("abc") | ||
val f1 = new FieldPosition(field, 1) | ||
assertEquals("java.text.FieldPosition[field=1,attribute=testsuite.javalib.text.FieldPositionTest$TestField(abc),beginIndex=0,endIndex=0]", f1.toString) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
testSuite/shared/src/test/scala/testsuite/javalib/text/ParsePositionTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package testsuite.javalib.text | ||
|
||
import java.text.{FieldPosition, Format, ParsePosition} | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
class ParsePositionTest { | ||
@Test def test_constructors(): Unit = { | ||
val p1 = new ParsePosition(1) | ||
assertEquals(1, p1.getIndex()) | ||
assertEquals(-1, p1.getErrorIndex()) | ||
} | ||
|
||
@Test def test_setters(): Unit = { | ||
val p1 = new ParsePosition(1) | ||
assertEquals(1, p1.getIndex()) | ||
assertEquals(-1, p1.getErrorIndex()) | ||
|
||
p1.setIndex(2) | ||
p1.setErrorIndex(2) | ||
|
||
assertEquals(2, p1.getIndex()) | ||
assertEquals(2, p1.getErrorIndex()) | ||
} | ||
|
||
@Test def test_equals_hash_code(): Unit = { | ||
val p1 = new ParsePosition(1) | ||
assertEquals(p1, p1) | ||
assertEquals(p1.hashCode(), p1.hashCode()) | ||
|
||
val p2 = new ParsePosition(1) | ||
assertEquals(p1, p2) | ||
assertEquals(p1.hashCode(), p2.hashCode()) | ||
|
||
val p3 = new ParsePosition(2) | ||
assertFalse(p1.equals(p3)) | ||
assertFalse(p1.hashCode() == p3.hashCode()) | ||
|
||
p1.setIndex(4) | ||
assertFalse(p1.equals(p2)) | ||
assertFalse(p1.hashCode() == p2.hashCode()) | ||
|
||
val p5 = new ParsePosition(1) | ||
val p6 = new ParsePosition(1) | ||
p5.setErrorIndex(1) | ||
assertFalse(p5.equals(p6)) | ||
assertFalse(p5.hashCode() == p6.hashCode()) | ||
} | ||
|
||
@Test def test_to_string(): Unit = { | ||
val p1 = new ParsePosition(1) | ||
assertEquals("java.text.ParsePosition[index=1,errorIndex=-1]", p1.toString) | ||
} | ||
} |