Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Aug 26, 2024
1 parent 1aa9774 commit dd6cccb
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 110 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/com/amazon/ionelement/api/IonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public fun IonElement.toIonValue(factory: ValueFactory): IonValue {
* Bridge function that converts from the mutable [IonValue] to an [AnyElement].
*
* New code that does not need to integrate with uses of the mutable DOM should not use this.
*
* This will fail for IonDatagram if the IonDatagram does not contain exactly one user value.
*/
public fun IonValue.toIonElement(): AnyElement =
this.system.newReader(this).use { reader ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ internal class IonElementLoaderImpl(private val options: IonElementLoaderOptions
return handleReaderException(ionReader) { loadCurrentElementRecursively(ionReader) }
}


private fun loadCurrentElementRecursively(ionReader: IonReader): AnyElement {
val valueType = requireNotNull(ionReader.type) { "The IonReader was not positioned at an element." }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,6 @@ internal fun <E> List<E>.toImmutableListUnsafe(): ImmutableList<E> {
return ImmutableListAdapter(this)
}

/**
* Creates a [ImmutableList] for [this] without making a defensive copy.
* Only call this method if you are sure that [this] cannot leak anywhere it could be mutated.
*/
internal fun <E> List<E>.toImmutableListUnsafeAllowModification(): ImmutableList<E> {
if (this is ImmutableList<E>) return this
return ImmutableListAdapter(this)
}

/**
* Creates a [ImmutableList] for [this] without making a defensive copy.
* Only call this method if you are sure that [this] cannot leak anywhere it could be mutated.
Expand All @@ -94,85 +85,3 @@ internal fun <E> Array<E>.toImmutableListUnsafe(): ImmutableList<E> {
// even further but using only a single wrapper layer, if we created such a thing.
return ImmutableListAdapter(asList())
}


internal interface SometimesMutableList<E> {
fun add(element: E): Boolean
fun seal(): ImmutableList<E>
}

internal class SometimesMutableListImpl<E> private constructor(
private val impl: MutableList<E>
) : SometimesMutableList<E>, ImmutableList<E>, List<E> by impl {
constructor(): this(mutableListOf())

private var isClosed = false

override fun add(element: E): Boolean {
if (isClosed) throw UnsupportedOperationException("Attempting to add $element")
impl.add(element)
return true
}

override fun seal(): ImmutableList<E> {
// check(!isClosed) { "Trying to seal an already sealed list" }
isClosed = true
return this
}

override fun subList(fromIndex: Int, toIndex: Int): ImmutableList<E> = ImmutableListAdapter(impl.subList(fromIndex, toIndex))

override fun equals(other: Any?): Boolean = impl.equals(other)
override fun hashCode(): Int = impl.hashCode()
override fun toString(): String = impl.toString()
}

internal class SealableList<E> : SometimesMutableList<E>, ImmutableList<E>, ArrayList<E>() {

private var isClosed = false

override fun add(element: E): Boolean {
if (isClosed) throw UnsupportedOperationException()
super.add(element)
return true
}

override fun seal(): ImmutableList<E> {
isClosed = true
return this
}

override fun subList(fromIndex: Int, toIndex: Int): Nothing {
TODO("Not supported due to conflicting interfaces")
}

// override fun subList(fromIndex: Int, toIndex: Int): List<E> = ImmutableListAdapter(subList(fromIndex, toIndex))
}

internal class SealableList2<E> : SometimesMutableList<E>, ArrayList<E>() {

private var isClosed = false

override fun add(element: E): Boolean {
if (isClosed) throw UnsupportedOperationException()
super.add(element)
return true
}

override fun seal(): ImmutableList<E> {
isClosed = true
return Immutable()
}

fun immutable(): ImmutableList<E> = Immutable()

override fun subList(fromIndex: Int, toIndex: Int): Nothing {
TODO("Not supported due to conflicting interfaces")
}

internal inner class Immutable: ImmutableList<E>, List<E> by this {
override fun subList(fromIndex: Int, toIndex: Int): ImmutableList<E> = ImmutableListAdapter(subList(fromIndex, toIndex))
}

// override fun subList(fromIndex: Int, toIndex: Int): List<E> = ImmutableListAdapter(subList(fromIndex, toIndex))
}
18 changes: 0 additions & 18 deletions src/test/kotlin/com/amazon/ionelement/IonElementLoaderTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,6 @@ class IonElementLoaderTests {
assertEquals(tc.expectedElement, parsedIonElement)
}

@Test
fun kotlinIddiomaticTest() {


val parsedIonValue = ION.loader.load("[null.struct, false]")
val parsedIonElement = loadAllElements("[null.struct, false]", INCLUDE_LOCATION_META)

// Text generated from both should match
// assertEquals(convertToString(parsedIonValue), parsedIonElement.toString())

// Converting from IonElement to IonValue results in an IonValue that is equivalent to the parsed IonValue
assertEquals(parsedIonElement.map { it.toIonValue(ION) }, parsedIonValue.toList())

// Converting from IonValue to IonElement should result in an IonElement that is equivalent to the
// parsed IonElement
// assertEquals(parsedIonValue.toIonElement(), parsedIonElement)
}

companion object {
@JvmStatic
@Suppress("unused")
Expand Down

0 comments on commit dd6cccb

Please sign in to comment.