Skip to content

Commit

Permalink
Convert MeasurementIdentifier to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0 committed Jun 24, 2024
1 parent 6a2af96 commit e5a027d
Show file tree
Hide file tree
Showing 3 changed files with 387 additions and 245 deletions.
142 changes: 0 additions & 142 deletions libs/model/src/main/java/de/cyface/model/MeasurementIdentifier.java

This file was deleted.

134 changes: 134 additions & 0 deletions libs/model/src/main/kotlin/de/cyface/model/MeasurementIdentifier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 2021-2024 Cyface GmbH
*
* This file is part of the Serialization.
*
* The Serialization is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Serialization is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Serialization. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cyface.model

import org.apache.commons.lang3.Validate
import java.io.Serializable

/**
* Describes the worldwide unique identifier of a measurement. This identifier consists of a device identifier and the
* actual measurement from that device.
*
* The natural order of [MeasurementIdentifier] is lexicographically by device identifier and then by
* measurement identifier.
*
* @author Klemens Muthmann
*/
class MeasurementIdentifier : Comparable<MeasurementIdentifier>, Serializable {
/**
* The worldwide unique identifier of the device that captured this measurement.
*/
private var deviceIdentifier: String? = null

/**
* The device wide unique identifier of the measurement.
*/
private var measurementIdentifier: Long = 0

/**
* The default no arguments constructor as required by Apache Flink to serialize and deserialize objects of this
* class. Do not use this in you own code, it creates an unusable [MeasurementIdentifier].
*/
constructor()

/**
* Creates a new completely initialized object of this class.
*
* @param deviceIdentifier The worldwide unique identifier of the device that captured this measurement
* @param measurementIdentifier The device wide unique identifier of the measurement
*/
constructor(deviceIdentifier: String?, measurementIdentifier: Long) {
setDeviceIdentifier(deviceIdentifier)
setMeasurementIdentifier(measurementIdentifier)
}

/**
* @return The worldwide unique identifier of the device that captured this measurement
*/
fun getDeviceIdentifier(): String? {
return deviceIdentifier
}

/**
* @return The device wide unique identifier of the measurement
*/
fun getMeasurementIdentifier(): Long {
return measurementIdentifier
}

/**
* @param deviceIdentifier The worldwide unique identifier of the device that captured this measurement
*/
@Suppress("MemberVisibilityCanBePrivate") // API
fun setDeviceIdentifier(deviceIdentifier: String?) {
Validate.notEmpty(deviceIdentifier)

this.deviceIdentifier = deviceIdentifier
}

/**
* @param measurementIdentifier The device wide unique identifier of the measurement
*/
@Suppress("MemberVisibilityCanBePrivate") // API
fun setMeasurementIdentifier(measurementIdentifier: Long) {
Validate.isTrue(measurementIdentifier >= 0)

this.measurementIdentifier = measurementIdentifier
}

override fun hashCode(): Int {
val prime = 31
var result = 1
result = prime * result + (if ((deviceIdentifier == null)) 0 else deviceIdentifier.hashCode())
result = prime * result + (measurementIdentifier xor (measurementIdentifier ushr 32)).toInt()
return result
}

override fun toString(): String {
return ("MeasurementIdentifier [deviceIdentifier=" + deviceIdentifier + ", measurementIdentifier="
+ measurementIdentifier + "]")
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null) return false
if (javaClass != other.javaClass) return false
val otherId = other as MeasurementIdentifier
if (deviceIdentifier == null) {
if (otherId.deviceIdentifier != null) return false
} else if (deviceIdentifier != otherId.deviceIdentifier) return false
return measurementIdentifier == otherId.measurementIdentifier
}

override fun compareTo(other: MeasurementIdentifier): Int {
val deviceIdentifierComparison = getDeviceIdentifier()!!
.compareTo(other.getDeviceIdentifier()!!)
return if (deviceIdentifierComparison == 0) this.getMeasurementIdentifier()
.compareTo(other.getMeasurementIdentifier())
else deviceIdentifierComparison
}

companion object {
/**
* Used to serialize objects of this class. Only change this value if this classes attribute set changes.
*/
@Suppress("ConstPropertyName")
private const val serialVersionUID = 181303400330020850L
}
}
Loading

0 comments on commit e5a027d

Please sign in to comment.