Skip to content

Commit

Permalink
add .toSome etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyre-S committed Jun 16, 2024
1 parent 2bdcca2 commit 742dd1f
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ code used in multiple projects.

Current features:

- Scala DSL for some commonly used math operator (`/-/`(over), `*^`(pow))
- Scala DSLs provide a way to do more simple jobs easier
- some commonly used math operator (`/-/`(over), `*^`(pow))
- extension methods to encapsulate values to scala base data types
- Library Tools
- GivenContext -— put(provide) and get(use) variables using its type as
the key
- Series random utils (currently only weighted boolean)
- Numeric Statistics from the Morny project.

Expand Down
92 changes: 92 additions & 0 deletions src/main/scala/data/ICompareRef.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/** This have been deprecated due to this have been implemented by Scala.
*/

package cc.sukazyo.std
package data

//trait ICompareRef [T <: AnyRef] (data: T) {
//
// /** Get this object's identity hash code.
// *
// * This hashcode should not be override by the class.
// *
// * It should always returns the same value on the same jvm object, no matter if its inner
// * status. And it should returns different values in different jvm object, even theirs
// * [[equals]] is true.
// *
// * But due to it is just a *hash code*, so it has the danger of collision makes it not
// * absolutely unique. So it may returns the same value for two different object in a very
// * unlucky situations.
// *
// * @see [[System.identityHashCode]] the provider of object's identity hash code
// */
// final def identityHashCode: Int =
// System.identityHashCode(data)
//
// /** Compare two objects by their identity hash code.
// *
// * This is relatively equals to `a == b` in native java, means it will compare if the two
// * objects given is **the same object in the jvm**.
// *
// * But due to it is just a *hash code*, so it has the danger of collision makes it not
// * absolutely unique. So it may occurs false-positive in a very unlucky situations.
// *
// * Take care of if this method (or [[identityHashCode]] method) have been overwritten,
// * this may cause this method cannot works as expected.
// *
// * @see [[identityHashCode]] the provider of object's identity hash code
// *
// * @param other Another object that want to compare with this object
// * @return `true` if the two objects are **the same object in the jvm**, false otherwise.
// *
// * Or in some very unlucky situations, it may return `true` even the two objects
// * are not the same.
// */
// infix final def sameAs (other: AnyRef): Boolean =
// data.identityHashCode == other.identityHashCode
// /** Compare two objects by their identity hash code.
// *
// * This is relatively equals to `a == b` in native java, means it will compare if the two
// * objects given is **the same object in the jvm**.
// *
// * But due to it is just a *hash code*, so it has the danger of collision makes it not
// * absolutely unique. So it may occurs false-positive in a very unlucky situations.
// *
// * Take care of if this method (or [[identityHashCode]] method) have been overwritten,
// * this may cause this method cannot works as expected.
// *
// * @see [[identityHashCode]] the provider of object's identity hash code
// *
// * @param other Another object (aka. rvalue) that want to compare with this object (aka.
// * lvalue)
// * @return `true` if the two objects are **the same object in the jvm**, false otherwise.
// *
// * Or in some very unlucky situations, it may return `true` even the two objects
// * are not the same.
// */
// infix final def =#= (other: AnyRef): Boolean =
// data sameAs other
// /** Compare two objects by their identity hash code.
// *
// * This is relatively equals to `a != b` in native java, means it will compare if the two
// * objects given is **the same object in the jvm**.
// *
// * But due to it is just a *hash code*, so it has the danger of collision makes it not
// * absolutely unique. So it may occurs false-positive in a very unlucky situations.
// *
// * Take care of if this method (or [[identityHashCode]] method) have been overwritten,
// * this may cause this method cannot works as expected.
// *
// * @see [[identityHashCode]] the provider of object's identity hash code
// *
// * @param other Another object (aka. rvalue) that want to compare with this object (aka.
// * lvalue)
// * @return `true` if the two objects are NOT **the same object in the jvm**, false otherwise.
// *
// * Or in some very unlucky situations, it may return `false` even the two objects
// * are not the same.
// */
// infix final def =!= (other: AnyRef): Boolean =
// !(data sameAs other)
//
//}
77 changes: 77 additions & 0 deletions src/main/scala/data/IEncapsulateValue.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cc.sukazyo.std
package data

trait IEncapsulateValue [T] (data: T) {

/** Convert this value `x: T` to a <code>[[Some]][T](x)</code>.
*
* The type `T` will be auto-deduced using the value x.
*
* @since 0.1.0
*/
def toSome: Some[T] = Some(data)

/** Convert this value `x: T` to a <code>[[Some]][T](x)</code>, but with the type
* <code>[[Option]][T]</code> returned.
*
* The type `T` will be auto-deduced using the value x.
*
* @since 0.1.0
*/
def toOption: Option[T] = this.toSome

/** Convert this value `x: T` to a <code>[[Left]][T, R](x)</code>.
*
* The type `T` will be auto-deduced using the value x.
*
* @tparam R The type of the [[Right]] value should be.
*
* It can be left empty, if so, the preferred type is [[Nothing]], makes it
* should be able to match any types.
*
* @since 0.1.0
*/
def toLeft[R]: Left[T, R] = Left(data)

/** Convert this value `x: T` to a <code>[[Right]][L, T](x)</code>.
*
* The type `T` will be auto-deduced using the value x.
*
* @tparam L The type of the [[Left]] value should be.
*
* It can be left empty, if so, the preferred type is [[Nothing]], makes it
* should be able to match any types.
*
* @since 0.1.0
*/
def toRight[L]: Right[L, T] = Right(data)

/** Convert this value `x: T` to a <code>[[Left]][T, R](x)</code>, but with the type <code>
* [[Either]][T, R]</code> returned.
*
* The type `T` will be auto-deduced using the value x.
*
* @tparam R The type of the [[Right]] value should be.
*
* It can be left empty, if so, the preferred type is [[Nothing]], makes it
* should be able to match any types.
*
* @since 0.1.0
*/
def toEitherLeft[R]: Either[T, R] = this.toLeft

/** Convert this value `x: T` to a <code>[[Right]][L, T](x)</code>, but with the type <code>
* [[Either]][T, R]</code> returned.
*
* The type `T` will be auto-deduced using the value x.
*
* @tparam L The type of the [[Left]] value should be.
*
* It can be left empty, if so, the preferred type is [[Nothing]], makes it
* should be able to match any types.
*
* @since 0.1.0
*/
def toEitherRight[L]: Either[L, T] = this.toRight

}
7 changes: 7 additions & 0 deletions src/main/scala/data/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cc.sukazyo.std

package object data {

implicit class EncapsulateValue [T] (data: T) extends IEncapsulateValue[T](data)

}
4 changes: 4 additions & 0 deletions src/test/scala/data/BaseDataTestSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cc.sukazyo.std
package data

class BaseDataTestSuite extends BaseTestsSuite
36 changes: 36 additions & 0 deletions src/test/scala/data/TestCompareRef.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cc.sukazyo.std
package data

//class TestCompareRef extends BaseDataTestSuite {
//
// "when using sameAs or =#= operator comparing two objects" - {
// val obj = "aaa"
// val objRef = obj
//
// //noinspection ScalaUnusedExpression
// "which is the same object in jvm should returns true" in {
// obj sameAs objRef shouldBe true
// obj === objRef shouldBe true
// obj =!= objRef shouldBe false
// }
//
// //noinspection ScalaUnusedExpression
// "which is different object in jvm should returns false, even they are Objects.equals" in {
// val objNew = new String("aaa")
// obj shouldEqual objNew
// obj sameAs objNew shouldBe false
// obj =#= objNew shouldBe false
// obj =!= objNew shouldBe true
// }
//
// //noinspection ScalaUnusedExpression
// "which is totally different object should returns false" in {
// val objOnSome = obj.toSome
// obj sameAs objOnSome shouldBe false
// obj =#= objOnSome shouldBe false
// obj =!= objOnSome shouldBe true
// }
//
// }
//
//}
64 changes: 64 additions & 0 deletions src/test/scala/data/TestEncapsulateValue.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cc.sukazyo.std
package data

class TestEncapsulateValue extends BaseDataTestSuite {

"any data" - {

"can be encapsulated to" - {

"a Some using .toSome extension" in {
val data = 42
val encapsulated = data.toSome
encapsulated shouldBe a [Some[?]]
encapsulated shouldEqual Some(42)
}

"an Option using .toOption extension" in {
val data = "String"
val encapsulated = data.toOption
encapsulated shouldBe a [Option[?]]
encapsulated shouldEqual Some("String")
}

"an Either" - {

"Left which itself is left-value using .toLeft extension" in {
val data = 42
val encapsulated = data.toLeft
assertCompiles("val left: Left[Int, String] = data.toLeft")
encapsulated shouldBe a [Left[?, ?]]
encapsulated shouldEqual Left(42)
}

"Right which itself is right-value using .toRight extension" in {
val data = "String"
val encapsulated = data.toRight
assertCompiles("val right: Right[Int, String] = data.toRight")
encapsulated shouldBe a [Right[?, ?]]
encapsulated shouldEqual Right("String")
}

"which itself is left-value using .toEitherLeft extension" in {
val data = 42
val encapsulated = data.toEitherLeft
assertCompiles("val either: Either[Int, String] = data.toEitherLeft")
encapsulated shouldBe a [Either[?, ?]]
encapsulated shouldEqual Left(42)
}

"which itself is right-value using .toEitherRight extension" in {
val data = "String"
val encapsulated = data.toEitherRight
assertCompiles("val either: Either[Int, String] = data.toEitherRight")
encapsulated shouldBe a [Either[?, ?]]
encapsulated shouldEqual Right("String")
}

}

}

}

}

0 comments on commit 742dd1f

Please sign in to comment.