This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/master by this push:
new 9277b8b WIP
9277b8b is described below
commit 9277b8becc3aa1b21805a2a6b21c9d9231d90c21
Author: Aaron Radzinski <[email protected]>
AuthorDate: Tue Oct 5 11:53:58 2021 -0700
WIP
---
.../org/apache/nlpcraft/common/util/NCUtils.scala | 86 ++++++++++++++++++++++
.../apache/nlpcraft/common/version/NCVersion.scala | 72 ++++++++++++++++++
2 files changed, 158 insertions(+)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index f512afe..daa3313 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -21,10 +21,13 @@ import com.typesafe.scalalogging.{LazyLogging, Logger}
import org.apache.nlpcraft.common.NCException
import org.apache.nlpcraft.common.ansi.NCAnsi.*
+import java.io.{IOException, InputStream, OutputStream}
+import java.net.{ServerSocket, Socket}
import java.util.Random
import java.util.regex.Pattern
import scala.annotation.tailrec
import scala.sys.SystemProperties
+import scala.util.control.Exception.ignoring
/**
*
@@ -559,3 +562,86 @@ object NCUtils extends LazyLogging:
*/
def interruptThread(t: Thread): Unit = if t != null then t.interrupt()
+ /**
+ * Checks duplicated elements in collection.
+ *
+ * @param list Collection. Note, it should be list.
+ * @param seen Checked elements.
+ * @see #getDups
+ */
+ @tailrec
+ def containsDups[T](list: List[T], seen: Set[T] = Set.empty[T]): Boolean =
+ list match
+ case x :: xs => if (seen.contains(x)) true else containsDups(xs,
seen + x)
+ case _ => false
+
+ /**
+ * Gets set of duplicate values from given sequence (potentially empty).
+ *
+ * @param seq Sequence to check for dups from.
+ * @tparam T
+ * @return
+ * @see #containsDups
+ */
+ def getDups[T](seq: Seq[T]): Set[T] = seq.diff(seq.distinct).toSet
+
+ /**
+ * Gets a sequence without dups. It works by checking for dups first,
before creating a new
+ * sequence if dups are found. It's more efficient when dups are rare.
+ *
+ * @param seq Sequence with potential dups.
+ */
+ def distinct[T](seq: List[T]): List[T] = if containsDups(seq) then
seq.distinct else seq
+
+ /**
+ * Safely and silently closes the client socket.
+ *
+ * @param sock Client socket to close.
+ */
+ def close(sock: Socket): Unit =
+ if sock != null then
+ ignoring(classOf[IOException]) {
+ sock.close()
+ }
+
+ /**
+ * Safely and silently closes the server socket.
+ *
+ * @param sock Server socket to close.
+ */
+ def close(sock: ServerSocket): Unit =
+ if sock != null then
+ ignoring(classOf[IOException]) {
+ sock.close()
+ }
+
+ /**
+ *
+ * @param in Stream.
+ */
+ def close(in: InputStream): Unit =
+ if in != null then
+ ignoring(classOf[IOException]) {
+ in.close()
+ }
+
+ /**
+ *
+ * @param out Stream.
+ */
+ def close(out: OutputStream): Unit =
+ if out != null then
+ ignoring(classOf[IOException]) {
+ out.close()
+ }
+
+ /**
+ * Closes auto-closeable ignoring any exceptions.
+ *
+ * @param a Resource to close.
+ */
+ def close(a: AutoCloseable): Unit =
+ if a != null then
+ ignoring(classOf[Exception]) {
+ a.close()
+ }
\ No newline at end of file
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/version/NCVersion.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/version/NCVersion.scala
new file mode 100644
index 0000000..9c9470d
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/version/NCVersion.scala
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nlpcraft.common.version
+
+import java.time.{LocalDate, Year}
+import com.typesafe.scalalogging.LazyLogging
+import org.apache.nlpcraft.common.*
+import org.apache.nlpcraft.common.util.NCUtils
+
+/**
+ * Release version holder. Note that this is manually changing property. For
every official
+ * release the new version will be added to this object manually.
+ */
+object NCVersion extends LazyLogging:
+ final val year = Year.now().toString
+ final val copyright = s"Copyright (C) $year Apache Software Foundation"
+ final val copyrightShort = s"(C) $year ASF"
+
+ /**
+ *
+ * @param version Semver-based release version of the NLPCraft.
+ * @param date Date of this release.
+ */
+ case class Version(
+ version: String, // Semver.
+ date: LocalDate
+ ):
+ override def toString = s"Version [version=$version, date=$date]"
+
+ // +=================================================+
+ // | UPDATE THIS SEQUENCE FOR EACH RELEASE MANUALLY. |
+ // +=================================================+
+ private final val VERSIONS = Seq(
+ Version("0.5.0", LocalDate.of(2020, 4, 16)),
+ Version("0.6.0", LocalDate.of(2020, 5, 25)),
+ Version("0.6.2", LocalDate.of(2020, 7, 9)),
+ Version("0.7.0", LocalDate.of(2020, 9, 29)),
+ Version("0.7.1", LocalDate.of(2020, 10, 29)),
+ Version("0.7.2", LocalDate.of(2020, 11, 19)),
+ Version("0.7.3", LocalDate.of(2020, 12, 31)),
+ Version("0.7.4", LocalDate.of(2021, 1, 31)),
+ Version("0.7.5", LocalDate.of(2021, 4, 30)),
+ Version("0.8.0", LocalDate.of(2021, 5, 30)),
+ Version("0.9.0", LocalDate.of(2021, 7, 10)),
+ ).sortBy(_.version)
+ // +=================================================+
+ // | UPDATE THIS SEQUENCE FOR EACH RELEASE MANUALLY. |
+ // +=================================================+
+
+ if
NCUtils.distinct(VERSIONS.map(_.version).toList).lengthCompare(VERSIONS.size)
!= 0 then
+ throw new AssertionError(s"Versions are NOT unique.")
+
+ /**
+ * Gets current version.
+ */
+ lazy val getCurrent: Version = VERSIONS.last
+