-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
286 additions
and
1 deletion.
There are no files selected for viewing
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
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,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) | ||
// | ||
//} |
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,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 | ||
|
||
} |
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 cc.sukazyo.std | ||
|
||
package object data { | ||
|
||
implicit class EncapsulateValue [T] (data: T) extends IEncapsulateValue[T](data) | ||
|
||
} |
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,4 @@ | ||
package cc.sukazyo.std | ||
package data | ||
|
||
class BaseDataTestSuite extends BaseTestsSuite |
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,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 | ||
// } | ||
// | ||
// } | ||
// | ||
//} |
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 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") | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |