Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currency instances #4564

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/src/main/scala-2.12/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class AllInstancesBinCompat
with AllInstancesBinCompat6
with AllInstancesBinCompat7
with AllInstancesBinCompat8
with AllInstancesBinCompat10

trait AllInstances
extends AnyValInstances
Expand Down Expand Up @@ -94,3 +95,5 @@ trait AllInstancesBinCompat7 extends SeqInstances
trait AllInstancesBinCompat8 extends DeadlineInstances

trait AllInstancesBinCompat9 extends InvariantInstances with InvariantInstancesBinCompat0

trait AllInstancesBinCompat10 extends CurrencyInstances
3 changes: 3 additions & 0 deletions core/src/main/scala-2.13+/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ abstract class AllInstancesBinCompat
with AllInstancesBinCompat7
with AllInstancesBinCompat8
with AllInstancesBinCompat9
with AllInstancesBinCompat11

trait AllInstances
extends AnyValInstances
Expand Down Expand Up @@ -99,3 +100,5 @@ trait AllInstancesBinCompat8 extends InvariantInstances
trait AllInstancesBinCompat9 extends DeadlineInstances

trait AllInstancesBinCompat10 extends InvariantInstancesBinCompat0

trait AllInstancesBinCompat11 extends CurrencyInstances
29 changes: 29 additions & 0 deletions core/src/main/scala/cats/instances/currency.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats
package instances

import java.util.Currency

trait CurrencyInstances extends cats.kernel.instances.CurrencyInstances {
implicit val catsStdShowForCurrency: Show[Currency] = Show.fromToString[Currency]
}
40 changes: 40 additions & 0 deletions kernel-laws/jvm/src/test/scala/cats/kernel/laws/JvmLawTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.kernel
package laws

import cats.kernel.compat.scalaVersionSpecific.*
import cats.kernel.instances.currency.*
import cats.kernel.laws.discipline.*
import munit.DisciplineSuite
import java.util.Currency
import org.scalacheck.{Arbitrary, Cogen, Gen}

class JvmLawTests extends TestsConfig with DisciplineSuite {
implicit private val arbitraryCurrency: Arbitrary[Currency] = Arbitrary(
Gen.oneOf(Currency.getAvailableCurrencies().asScala)
)
implicit private val cogenCurrency: Cogen[Currency] = Cogen[String].contramap(_.getCurrencyCode())

checkAll("Eq[Currency]", EqTests[Currency].eqv)
checkAll("Hash[Currency]", HashTests[Currency].hash)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

package cats.kernel.compat
import scala.annotation.{Annotation, StaticAnnotation}
import scala.collection.{IterableLike, TraversableLike}
import scala.collection.{mutable, IterableLike, TraversableLike}
import scala.collection.convert.ImplicitConversionsToScala.*

private[cats] object scalaVersionSpecific {

Expand Down Expand Up @@ -50,4 +51,8 @@ private[cats] object scalaVersionSpecific {
that: T
)(implicit w1: A => TraversableLike[El1, Repr1], w2: T => IterableLike[El2, Repr2]) = (a, that).zipped
}

implicit class setExtension[A](private val s: java.util.Set[A]) extends AnyVal {
def asScala: mutable.Set[A] = `set asScala`(s)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extension method is only used for Gen.oneOf in tests, and the latter only requires Iterable. So I'd suggest to go with the minimally required type here as well:

Suggested change
implicit class setExtension[A](private val s: java.util.Set[A]) extends AnyVal {
def asScala: mutable.Set[A] = `set asScala`(s)
}
implicit class iterableExtension[A](private val s: java.lang.Iterable[A]) extends AnyVal {
def asScala: Iterable[A] = iterableAsScalaIterable(s) // assuming import scala.collection.JavaConverters._
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides, if this extension method is for the sake of testing only, I wonder if it'd make sense to move it from "main" to "test" scope?

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ private[instances] trait AllInstancesBinCompat0 extends FiniteDurationInstances
private[instances] trait AllInstancesBinCompat1 extends SortedMapInstances with SortedSetInstances

private[instances] trait AllInstancesBinCompat2 extends DeadlineInstances

private[instances] trait AllInstancesBinCompat3 extends CurrencyInstances
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package kernel

package compat
import scala.annotation.{Annotation, StaticAnnotation}
import scala.collection.mutable
import scala.jdk.CollectionConverters.*

private[cats] object scalaVersionSpecific {

Expand All @@ -35,4 +37,8 @@ private[cats] object scalaVersionSpecific {
implicit class iterableOnceExtension[A](private val io: IterableOnce[A]) extends AnyVal {
def reduceOption(f: (A, A) => A): Option[A] = io.iterator.reduceOption(f)
}

implicit class setExtension[A](private val s: java.util.Set[A]) extends AnyVal {
def asScala: mutable.Set[A] = SetHasAsScala(s).asScala
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that SetHasAsScala is a part of any API. Not mention this call can allocate a class instance without a reason. I'd probably give a shot to scala.jdk.javaapi.CollectionConverters here, e.g.:

import scala.jdk.javaapi.CollectionConverters

...

def asScala: Iterable[A] = CollectionConverters.asScala(x)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ private[instances] trait AllInstancesBinCompat0 extends FiniteDurationInstances
private[instances] trait AllInstancesBinCompat1 extends SortedMapInstances with SortedSetInstances

private[instances] trait AllInstancesBinCompat2 extends DeadlineInstances

private[instances] trait AllInstancesBinCompat3 extends CurrencyInstances
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.kernel
package instances

import java.util.Currency

trait CurrencyInstances {
implicit val catsKernelStdHashForCurrency: Hash[Currency] = Hash.fromUniversalHashCode[Currency]
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ package object all
with AllInstancesBinCompat0
with AllInstancesBinCompat1
with AllInstancesBinCompat2
with AllInstancesBinCompat3
25 changes: 25 additions & 0 deletions kernel/src/main/scala/cats/kernel/instances/currency/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.kernel
package instances

package object currency extends CurrencyInstances