kandi background
Explore Kits

java-util | Rarely available and hard-to-write Java utilities | Assertion library

 by   jdereg Java Version: 2.0.0 License: Apache-2.0

 by   jdereg Java Version: 2.0.0 License: Apache-2.0

kandi X-RAY | java-util Summary

java-util is a Java library typically used in Testing, Assertion applications. java-util has no bugs, it has no vulnerabilities, it has build file available, it has a Permissive License and it has low support. You can download it from GitHub, Maven.
Rarely available and hard-to-write Java utilities, written correctly, and thoroughly tested (> 98% code coverage via JUnit tests).
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • java-util has a low active ecosystem.
  • It has 347 star(s) with 97 fork(s). There are 48 watchers for this library.
  • It had no major release in the last 12 months.
  • There are 3 open issues and 11 have been closed. On average issues are closed in 125 days. There are no pull requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of java-util is 2.0.0
java-util Support
Best in #Assertion
Average in #Assertion
java-util Support
Best in #Assertion
Average in #Assertion

quality kandi Quality

  • java-util has 0 bugs and 0 code smells.
java-util Quality
Best in #Assertion
Average in #Assertion
java-util Quality
Best in #Assertion
Average in #Assertion

securitySecurity

  • java-util has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • java-util code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
java-util Security
Best in #Assertion
Average in #Assertion
java-util Security
Best in #Assertion
Average in #Assertion

license License

  • java-util is licensed under the Apache-2.0 License. This license is Permissive.
  • Permissive licenses have the least restrictions, and you can use them in most projects.
java-util License
Best in #Assertion
Average in #Assertion
java-util License
Best in #Assertion
Average in #Assertion

buildReuse

  • java-util releases are not available. You will need to build from source code and install.
  • Deployable package is available in Maven.
  • Build file is available. You can build the component from source.
  • Installation instructions are not available. Examples and code snippets are available.
  • It has 20840 lines of code, 1256 functions and 71 files.
  • It has high code complexity. Code complexity directly impacts maintainability of the code.
java-util Reuse
Best in #Assertion
Average in #Assertion
java-util Reuse
Best in #Assertion
Average in #Assertion
Top functions reviewed by kandi - BETA

kandi has reviewed java-util and discovered the below as its top functions. This is intended to give you an instant insight into java-util implemented functionality, and help decide if they suit your requirements.

  • Parse a date from a string .
    • Compares two delta objects in the given source and target objects .
      • Get a set of all entries in this map .
        • Returns the damerau - Levenshtein distance between two strings .
          • Converts an instance to a BigDecimal .
            • Deep hash code .
              • Traverse the given object .
                • Obtains the cookies from the URLConnection .
                  • Executes a HTTP POST request .
                    • Adds an item to the set .

                      Get all kandi verified functions for this library.

                      Get all kandi verified functions for this library.

                      java-util Key Features

                      Rarely available and hard-to-write Java utilities, written correctly, and thoroughly tested

                      java-util Examples and Code Snippets

                      See all related Code Snippets

                      Community Discussions

                      Trending Discussions on java-util
                      • Platform class java.util.Currency requires explicit JsonAdapter to be registered
                      • Modelmapper failed to convert java.lang.String to java.sql.Timestamp
                      • Spring Boot Logging to a File
                      • Could not resolve com.google.guava:guava:30.1-jre - Gradle project sync failed. Basic functionality will not work properly - in kotlin project
                      • Does Map store copy of the reference variable or copy of the object?
                      • Jackson TypeReference with dynamic method parameter type
                      • Can not convert XML to Map with XStream
                      • Dependency convergence error while validating Hazelcast project
                      • Serializing using Rest Assured response.jsonPath().getObject() returns null
                      • running flutter project for first time
                      Trending Discussions on java-util

                      QUESTION

                      Platform class java.util.Currency requires explicit JsonAdapter to be registered

                      Asked 2022-Apr-10 at 05:44

                      Getting error while using Moshi code gen for data class with Currency.

                      Referred similar questions. (Adding referred question at the end)

                      Error

                      Caused by: java.lang.IllegalArgumentException: Platform class java.util.Currency requires explicit JsonAdapter to be registered

                      Moshi Builder

                      private val moshi = Moshi.Builder()
                          .add(CurrencyJsonAdapter())
                          // .add(KotlinJsonAdapterFactory())
                          .build()
                      private val initialDatabaseDataJsonAdapter: JsonAdapter<InitialDatabaseData> =
                          moshi.adapter(InitialDatabaseData::class.java)
                      

                      InitialDatabaseData is a data class that has val sources: List<Source>, as one of its fields.
                      Source data class in turn has,

                      @field:Json(name = "balance_amount")
                      val balanceAmount: Amount = Amount(
                          value = 0,
                      ),
                      

                      Amount data class

                      import com.squareup.moshi.JsonClass
                      import java.util.Currency
                      
                      const val CURRENCY_CODE_INR = "INR"
                      
                      @JsonClass(generateAdapter = true)
                      data class Amount(
                          val currency: Currency = Currency.getInstance(CURRENCY_CODE_INR),
                          val value: Long = 0,
                      ) {
                          fun toSignedString(): String {
                              val formattedValue = formattedCurrencyValue(
                                  value = value,
                              )
                              return if (value > 0) {
                                  "${currency.symbol} +$formattedValue"
                              } else {
                                  "${currency.symbol} $formattedValue"
                              }
                          }
                      
                          override fun toString(): String {
                              val formattedValue = formattedCurrencyValue(
                                  value = value,
                              )
                              return "${currency.symbol} $formattedValue"
                          }
                      
                          operator fun plus(
                              amount: Amount,
                          ): Amount {
                              return Amount(
                                  currency = currency,
                                  value = value + amount.value,
                              )
                          }
                      }
                      

                      Custom Adapter for Currency

                      import com.squareup.moshi.FromJson
                      import com.squareup.moshi.ToJson
                      import java.util.Currency
                      
                      class CurrencyJsonAdapter {
                          @ToJson
                          fun toJson(
                              currency: Currency,
                          ): String {
                              return currency.currencyCode
                          }
                      
                          @FromJson
                          fun fromJson(
                              currencyCodeString: String,
                          ): Currency {
                              return Currency.getInstance(currencyCodeString)
                          }
                      }
                      

                      Dependencies

                      implementation "com.squareup.moshi:moshi:1.13.0"
                      implementation "com.squareup.moshi:moshi-kotlin:1.9.3"
                      kapt "com.squareup.moshi:moshi-kotlin-codegen:1.13.0"
                      

                      Note:

                      1. Not looking for solutions using kotlin reflection. (Adding .add(KotlinJsonAdapterFactory()) to Moshi builder)
                      2. Please add a comment if anything else is required.

                      Complete Error (if required)

                      java.lang.ExceptionInInitializerError
                              at com.makeappssimple.abhimanyu.financemanager.android.utils.JsonUtilKt.readInitialDataFromAssets(Unknown Source:0)
                              at com.makeappssimple.abhimanyu.financemanager.android.data.local.database.MyRoomDatabase$Companion$populateInitialData$1$1.invokeSuspend(MyRoomDatabase.kt:176)
                              at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                              at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
                              at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
                              at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
                              at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
                              at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
                              at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
                              at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
                           Caused by: java.lang.IllegalArgumentException: Platform class java.util.Currency requires explicit JsonAdapter to be registered
                          for class java.util.Currency currency
                          for class com.makeappssimple.abhimanyu.financemanager.android.entities.amount.Amount balanceAmount
                          for class com.makeappssimple.abhimanyu.financemanager.android.entities.source.Source
                          for java.util.List<com.makeappssimple.abhimanyu.financemanager.android.entities.source.Source> sources
                          for class com.makeappssimple.abhimanyu.financemanager.android.entities.initialdatabasedata.InitialDatabaseData
                              at com.squareup.moshi.Moshi$LookupChain.exceptionWithLookupStack(Moshi.java:389)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:158)
                              at com.makeappssimple.abhimanyu.financemanager.android.entities.amount.AmountJsonAdapter.<init>(AmountJsonAdapter.kt:30)
                              at java.lang.reflect.Constructor.newInstance0(Native Method)
                              at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
                              at com.squareup.moshi.internal.Util.generatedAdapter(Util.java:588)
                              at com.squareup.moshi.StandardJsonAdapters$1.create(StandardJsonAdapters.java:61)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
                              at com.makeappssimple.abhimanyu.financemanager.android.entities.source.SourceJsonAdapter.<init>(SourceJsonAdapter.kt:30)
                              at java.lang.reflect.Constructor.newInstance0(Native Method)
                              at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
                              at com.squareup.moshi.internal.Util.generatedAdapter(Util.java:588)
                              at com.squareup.moshi.StandardJsonAdapters$1.create(StandardJsonAdapters.java:61)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:106)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:75)
                              at com.squareup.moshi.CollectionJsonAdapter.newArrayListAdapter(CollectionJsonAdapter.java:54)
                              at com.squareup.moshi.CollectionJsonAdapter$1.create(CollectionJsonAdapter.java:38)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
                              at com.makeappssimple.abhimanyu.financemanager.android.entities.initialdatabasedata.InitialDatabaseDataJsonAdapter.<init>(InitialDatabaseDataJsonAdapter.kt:31)
                              at java.lang.reflect.Constructor.newInstance0(Native Method)
                              at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
                              at com.squareup.moshi.internal.Util.generatedAdapter(Util.java:588)
                              at com.squareup.moshi.StandardJsonAdapters$1.create(StandardJsonAdapters.java:61)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:106)
                              at com.squareup.moshi.Moshi.adapter(Moshi.java:80)
                              at com.makeappssimple.abhimanyu.financemanager.android.utils.JsonUtilKt.<clinit>(JsonUtil.kt:24)
                                  ... 10 more
                           Caused by: java.lang.IllegalArgumentException: Platform class java.util.Currency requires explicit JsonAdapter to be registered
                              at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:76)
                      2022-04-05 03:57:46.008 E/AndroidRuntime:     at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
                                  ... 36 more
                      
                      private val moshi = Moshi.Builder()
                          .add(CurrencyJsonAdapter())
                          .build()
                      private val initialDatabaseDataJsonAdapter: JsonAdapter<InitialDatabaseData> =
                          moshi.adapter(InitialDatabaseData::class.java)
                      private val databaseBackupDataJsonAdapter: JsonAdapter<DatabaseBackupData> =
                          moshi.adapter(DatabaseBackupData::class.java)
                      
                      class JsonUtil @Inject constructor(
                          @ApplicationContext private val context: Context,
                      ) {
                          fun readDatabaseBackupDataFromFile(
                              uri: Uri,
                          ): DatabaseBackupData? {
                              val contentResolver = context.contentResolver
                      
                              val stringBuilder = StringBuilder()
                              contentResolver.openInputStream(uri)?.use { inputStream ->
                                  BufferedReader(InputStreamReader(inputStream)).use { bufferedReader ->
                                      var line: String? = bufferedReader.readLine()
                                      while (line != null) {
                                          stringBuilder.append(line)
                                          line = bufferedReader.readLine()
                                      }
                                  }
                              }
                              return databaseBackupDataJsonAdapter.fromJson(stringBuilder.toString())
                          }
                      
                          fun writeDatabaseBackupDataToFile(
                              uri: Uri,
                              databaseBackupData: DatabaseBackupData,
                          ) {
                              val jsonString = databaseBackupDataJsonAdapter.toJson(databaseBackupData)
                              writeJsonToFile(
                                  uri = uri,
                                  jsonString = jsonString,
                              )
                          }
                      
                          private fun writeJsonToFile(
                              uri: Uri,
                              jsonString: String,
                          ) {
                              val contentResolver = context.contentResolver
                              try {
                                  contentResolver.openFileDescriptor(uri, "w")?.use {
                                      FileOutputStream(it.fileDescriptor).use { fileOutputStream ->
                                          fileOutputStream.write(jsonString.toByteArray())
                                      }
                                  }
                              } catch (fileNotFoundException: FileNotFoundException) {
                                  fileNotFoundException.printStackTrace()
                              } catch (ioException: IOException) {
                                  ioException.printStackTrace()
                              }
                          }
                      }
                      
                      fun readInitialDataFromAssets(
                          context: Context,
                      ): InitialDatabaseData? {
                          val jsonString = readJsonFileFromAssets(
                              context = context,
                              fileName = "database/initial_data.json",
                          ) ?: return null
                          return initialDatabaseDataJsonAdapter.fromJson(jsonString)
                      }
                      
                      @Suppress("SameParameterValue")
                      private fun readJsonFileFromAssets(
                          context: Context,
                          fileName: String,
                      ): String? {
                          val json = try {
                              val inputStream: InputStream = context.assets.open(fileName)
                              val size: Int = inputStream.available()
                              val byteArray = ByteArray(size)
                              inputStream.read(byteArray)
                              inputStream.close()
                              String(
                                  bytes = byteArray,
                                  charset = Charset.forName("UTF-8"),
                              )
                          } catch (ioException: IOException) {
                              ioException.printStackTrace()
                              null
                          }
                          return json
                      }
                      

                      MyRoomDatabase.kt

                      @Database( version = 14, entities = [ Category::class, EmojiLocalEntity::class, Source::class, Transaction::class, ], exportSchema = true, ) @TypeConverters( AmountConverter::class, CategoryIdsConverter::class, CategoryConverter::class, ) abstract class MyRoomDatabase : RoomDatabase() { // Dao declarations

                      companion object {
                          @Volatile
                          private var INSTANCE: MyRoomDatabase? = null
                      
                          fun getDatabase(
                              context: Context,
                          ): MyRoomDatabase {
                              val tempInstance = INSTANCE
                              if (tempInstance != null) {
                                  // tempInstance.populateInitialData()
                                  return tempInstance
                              }
                              synchronized(
                                  lock = this,
                              ) {
                                  val callback: Callback = object : Callback() {
                                      override fun onCreate(
                                          supportSQLiteDatabase: SupportSQLiteDatabase,
                                      ) {
                                          // do something after database has been created
                                      }
                      
                                      override fun onOpen(
                                          supportSQLiteDatabase: SupportSQLiteDatabase,
                                      ) {
                                          // do something every time database is open
                                          Executors
                                              .newSingleThreadScheduledExecutor()
                                              .execute {
                                                  populateInitialData(
                                                      context = context,
                                                  )
                                              }
                                      }
                                  }
                      
                                  val instance = Room
                                      .databaseBuilder(
                                          context.applicationContext,
                                          MyRoomDatabase::class.java,
                                          "finance_manager_database",
                                      )
                                      .addCallback(callback)
                                      .build()
                                  INSTANCE = instance
                                  return instance
                              }
                          }
                      
                          private fun populateInitialData(
                              context: Context,
                          ) {
                              val myRoomDatabase = getDatabase(
                                  context = context,
                              )
                              myRoomDatabase.runInTransaction {
                                  CoroutineScope(
                                      context = Dispatchers.IO,
                                  ).launch {
                                      val initialDatabaseData = readInitialDataFromAssets(
                                          context = context,
                                      )
                                      initialDatabaseData?.let {
                                          // Do required operations
                                      }
                                  }
                              }
                          }
                      }
                      

                      }

                      ANSWER

                      Answered 2022-Apr-10 at 05:44

                      I was able to figure out the issue.

                      The root cause was the default AmountJsonAdapter created by Moshi was not internally using the CurrencyJsonAdapter.

                      Changes required,
                      1. Create custom JSON adapter for Amount rather than Currency.
                      2. Add AmountJsonAdapter() instead of CurrencyJsonAdapter() in Moshi.Builder().
                      3. Disable moshi adapter generation for Amount.
                      After changes,

                      Moshi Builder

                      private val moshi = Moshi.Builder()
                          .add(AmountJsonAdapter())
                          .build()
                      

                      AmountJsonAdapter

                      @JsonClass(generateAdapter = true)
                      data class AmountJson(
                          val currency: String,
                          val value: Long = 0,
                      )
                      
                      class AmountJsonAdapter {
                          @ToJson
                          fun toJson(
                              amount: Amount,
                          ): AmountJson {
                              return AmountJson(
                                  currency = amount.currency.currencyCode,
                                  value = amount.value,
                              )
                          }
                      
                          @FromJson
                          fun fromJson(
                              amountJson: AmountJson,
                          ): Amount {
                              return Amount(
                                  currency = Currency.getInstance(amountJson.currency),
                                  value = amountJson.value,
                              )
                          }
                      }
                      

                      Amount.kt

                      @JsonClass(generateAdapter = false)
                      data class Amount(
                        // Same code
                      )
                      

                      Source https://stackoverflow.com/questions/71811182

                      Community Discussions, Code Snippets contain sources that include Stack Exchange Network

                      Vulnerabilities

                      No vulnerabilities reported

                      Install java-util

                      You can download it from GitHub, Maven.
                      You can use java-util like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the java-util component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

                      Support

                      For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

                      Find more information at:

                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
                      over 650 million Knowledge Items
                      Find more libraries
                      Reuse Solution Kits and Libraries Curated by Popular Use Cases
                      Explore Kits

                      Save this library and start creating your kit

                      Install
                      Clone
                      • https://github.com/jdereg/java-util.git

                      • gh repo clone jdereg/java-util

                      • git@github.com:jdereg/java-util.git

                      Share this Page

                      share link

                      See Similar Libraries in

                      Consider Popular Assertion Libraries
                      Try Top Libraries by jdereg
                      Compare Assertion Libraries with Highest Support
                      Compare Assertion Libraries with Highest Quality
                      Compare Assertion Libraries with Highest Security
                      Compare Assertion Libraries with Permissive License
                      Compare Assertion Libraries with Highest Reuse
                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
                      over 650 million Knowledge Items
                      Find more libraries
                      Reuse Solution Kits and Libraries Curated by Popular Use Cases
                      Explore Kits

                      Save this library and start creating your kit