Skip to content

Commit b5f547d

Browse files
committed
Add equals and hashcode
1 parent a295fef commit b5f547d

File tree

10 files changed

+135
-13
lines changed

10 files changed

+135
-13
lines changed

src/main/kotlin/com/valaphee/blit/data/config/Config.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.valaphee.blit.data.config
1818

1919
import com.fasterxml.jackson.annotation.JsonIgnore
2020
import com.fasterxml.jackson.annotation.JsonValue
21+
import com.google.inject.Inject
2122
import com.google.inject.Singleton
2223
import com.valaphee.blit.data.Data
2324
import com.valaphee.blit.data.DataType
@@ -89,7 +90,10 @@ class Config(
8990
@get:JsonIgnore internal val sourcesProperty = SimpleListProperty(sources.asObservable())
9091
var sources: ObservableList<SourceConfig> by sourcesProperty
9192

92-
class Model : ItemViewModel<Config>() {
93+
@Singleton
94+
class Model @Inject constructor(
95+
config: Config
96+
) : ItemViewModel<Config>(config) {
9397
val locale = bind(Config::localeProperty)
9498
val dataSizeUnit = bind(Config::dataSizeUnitProperty)
9599
val temporaryPath = bind(Config::temporaryPathProperty)

src/main/kotlin/com/valaphee/blit/data/config/ConfigView.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import tornadofx.View
3030
import tornadofx.action
3131
import tornadofx.button
3232
import tornadofx.buttonbar
33+
import tornadofx.enableWhen
3334
import tornadofx.select
3435
import tornadofx.tab
3536
import tornadofx.tabpane
@@ -42,7 +43,7 @@ import kotlin.reflect.KClass
4243
*/
4344
class ConfigView : View("Configure Blit") {
4445
private val locale by di<Locale>()
45-
private val _config by di<Config>()
46+
private val configModel by di<Config.Model>()
4647

4748
private var tabs = mutableMapOf<KClass<out Component>, Tab>()
4849

@@ -63,19 +64,19 @@ class ConfigView : View("Configure Blit") {
6364
buttonbar {
6465
button(locale["config.ok.text"]) {
6566
action {
66-
//_config.commit()
67+
configModel.commit()
6768
(scene.window as Stage).close()
6869
}
6970
}
7071
button(locale["config.cancel.text"]) {
7172
action {
72-
//_config.rollback()
73+
configModel.rollback()
7374
(scene.window as Stage).close()
7475
}
7576
}
7677
button(locale["config.apply.text"]) {
77-
//enableWhen(_config.dirty)
78-
//action(_config::commit)
78+
enableWhen(configModel.dirty)
79+
action(configModel::commit)
7980
}
8081
}
8182
}

src/main/kotlin/com/valaphee/blit/data/config/ConfigViewGeneral.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ import java.io.File
3333
*/
3434
class ConfigViewGeneral : Fragment("General") {
3535
private val locale by di<Locale>()
36-
private val _config by di<Config>()
36+
private val configModel by di<Config.Model>()
3737
private val injector by di<Injector>()
3838

3939
override val root = form {
4040
fieldset {
4141
val locales = injector.getInstance(object : Key<Map<String, @JvmSuppressWildcards Locale>>() {})
42-
field(locale["config.general.locale.text"]) { combobox(_config.localeProperty, locales.keys.toList()) { cellFormat { text = locales[it]!!["name"] } } }
43-
field(locale["config.general.data_size_unit.text"]) { combobox(_config.dataSizeUnitProperty, Config.DataSizeUnit.values().toList()) { cellFormat { text = locale["config.general.data_size_unit.${it.key}"] } } }
44-
field(locale["config.general.temporary_path.text"]) { textfield(_config.temporaryPathProperty) { validator { if (it.isNullOrBlank() || !File(it).isDirectory) error(locale["config.general.temporary_path.invalid"]) else null } } }
42+
field(locale["config.general.locale.text"]) { combobox(configModel.locale, locales.keys.toList()) { cellFormat { text = locales[it]!!["name"] } } }
43+
field(locale["config.general.data_size_unit.text"]) { combobox(configModel.dataSizeUnit, Config.DataSizeUnit.values().toList()) { cellFormat { text = locale["config.general.data_size_unit.${it.key}"] } } }
44+
field(locale["config.general.temporary_path.text"]) { textfield(configModel.temporaryPath) { validator { if (it.isNullOrBlank() || !File(it).isDirectory) error(locale["config.general.temporary_path.invalid"]) else null } } }
4545
}
4646
}
4747
}

src/main/kotlin/com/valaphee/blit/data/config/ConfigViewSources.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ import tornadofx.vgrow
3838
*/
3939
class ConfigViewSources : Fragment("Sources") {
4040
private val locale by di<Locale>()
41-
private val _config by di<Config>()
41+
private val configModel by di<Config.Model>()
4242

4343
private val source = SimpleObjectProperty<SourceConfig>()
4444

4545
override val root = hbox {
4646
vbox {
47-
add(listview(_config.sources) {
47+
add(listview(configModel.sources) {
4848
bindSelected(source)
4949

5050
vgrow = Priority.ALWAYS

src/main/kotlin/com/valaphee/blit/source/SourceConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ abstract class SourceConfig(
4848
name: String
4949
) {
5050
@get:JsonIgnore protected val nameProperty = SimpleStringProperty(name)
51-
var name by nameProperty
51+
var name: String by nameProperty
5252

5353
abstract fun newUi(eventTarget: EventTarget)
5454

src/main/kotlin/com/valaphee/blit/source/dav/DavSourceConfig.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,30 @@ class DavSourceConfig(
7070
}
7171

7272
override fun newSource() = DavSource(nameProperty.value, urlProperty.value, usernameProperty.value, passwordProperty.value, nextcloudProperty.value, nextcloudUploadChunkSizeProperty.value)
73+
74+
override fun equals(other: Any?): Boolean {
75+
if (this === other) return true
76+
if (javaClass != other?.javaClass) return false
77+
78+
other as DavSourceConfig
79+
80+
if (name != other.name) return false
81+
if (url != other.url) return false
82+
if (username != other.username) return false
83+
if (password != other.password) return false
84+
if (nextcloud != other.nextcloud) return false
85+
if (nextcloudUploadChunkSize != other.nextcloudUploadChunkSize) return false
86+
87+
return true
88+
}
89+
90+
override fun hashCode(): Int {
91+
var result = name.hashCode()
92+
result = 31 * result + url.hashCode()
93+
result = 31 * result + username.hashCode()
94+
result = 31 * result + password.hashCode()
95+
result = 31 * result + nextcloud.hashCode()
96+
result = 31 * result + nextcloudUploadChunkSize.hashCode()
97+
return result
98+
}
7399
}

src/main/kotlin/com/valaphee/blit/source/k8scp/K8scpSourceConfig.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,24 @@ class K8scpSourceConfig(
4949
}
5050

5151
override fun newSource() = K8scpSource(nameProperty.value, namespaceProperty.value, podProperty.value)
52+
53+
override fun equals(other: Any?): Boolean {
54+
if (this === other) return true
55+
if (javaClass != other?.javaClass) return false
56+
57+
other as K8scpSourceConfig
58+
59+
if (name != other.name) return false
60+
if (namespace != other.namespace) return false
61+
if (pod != other.pod) return false
62+
63+
return true
64+
}
65+
66+
override fun hashCode(): Int {
67+
var result = name.hashCode()
68+
result = 31 * result + namespace.hashCode()
69+
result = 31 * result + pod.hashCode()
70+
return result
71+
}
5272
}

src/main/kotlin/com/valaphee/blit/source/local/LocalSourceConfig.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.valaphee.blit.source.local
1818

1919
import com.fasterxml.jackson.annotation.JsonTypeName
2020
import com.valaphee.blit.source.SourceConfig
21+
import com.valaphee.blit.source.k8scp.K8scpSourceConfig
2122
import javafx.event.EventTarget
2223
import tornadofx.field
2324
import tornadofx.textfield
@@ -36,4 +37,17 @@ class LocalSourceConfig(
3637
}
3738

3839
override fun newSource() = LocalSource(nameProperty.value)
40+
41+
override fun equals(other: Any?): Boolean {
42+
if (this === other) return true
43+
if (javaClass != other?.javaClass) return false
44+
45+
other as K8scpSourceConfig
46+
47+
if (name != other.name) return false
48+
49+
return true
50+
}
51+
52+
override fun hashCode() = name.hashCode()
3953
}

src/main/kotlin/com/valaphee/blit/source/scp/ScpSourceConfig.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,32 @@ class ScpSourceConfig(
8484
}
8585

8686
override fun newSource() = ScpSource(nameProperty.value, hostProperty.value, portProperty.value, usernameProperty.value, passwordProperty.value, privateKeyProperty.value, connectionPoolSizeProperty.value)
87+
88+
override fun equals(other: Any?): Boolean {
89+
if (this === other) return true
90+
if (javaClass != other?.javaClass) return false
91+
92+
other as ScpSourceConfig
93+
94+
if (name != other.name) return false
95+
if (host != other.host) return false
96+
if (port != other.port) return false
97+
if (username != other.username) return false
98+
if (password != other.password) return false
99+
if (privateKey != other.privateKey) return false
100+
if (connectionPoolSize != other.connectionPoolSize) return false
101+
102+
return true
103+
}
104+
105+
override fun hashCode(): Int {
106+
var result = name.hashCode()
107+
result = 31 * result + host.hashCode()
108+
result = 31 * result + port.hashCode()
109+
result = 31 * result + username.hashCode()
110+
result = 31 * result + password.hashCode()
111+
result = 31 * result + privateKey.hashCode()
112+
result = 31 * result + connectionPoolSize.hashCode()
113+
return result
114+
}
87115
}

src/main/kotlin/com/valaphee/blit/source/sftp/SftpSourceConfig.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.valaphee.blit.source.sftp
1818

1919
import com.fasterxml.jackson.annotation.JsonTypeName
2020
import com.valaphee.blit.source.SourceConfig
21+
import com.valaphee.blit.source.scp.ScpSourceConfig
2122
import javafx.beans.property.SimpleIntegerProperty
2223
import javafx.beans.property.SimpleStringProperty
2324
import javafx.event.EventTarget
@@ -84,4 +85,32 @@ class SftpSourceConfig(
8485
}
8586

8687
override fun newSource() = SftpSource(nameProperty.value, hostProperty.value, portProperty.value, usernameProperty.value, passwordProperty.value, privateKeyProperty.value, connectionPoolSizeProperty.value)
88+
89+
override fun equals(other: Any?): Boolean {
90+
if (this === other) return true
91+
if (javaClass != other?.javaClass) return false
92+
93+
other as ScpSourceConfig
94+
95+
if (name != other.name) return false
96+
if (host != other.host) return false
97+
if (port != other.port) return false
98+
if (username != other.username) return false
99+
if (password != other.password) return false
100+
if (privateKey != other.privateKey) return false
101+
if (connectionPoolSize != other.connectionPoolSize) return false
102+
103+
return true
104+
}
105+
106+
override fun hashCode(): Int {
107+
var result = name.hashCode()
108+
result = 31 * result + host.hashCode()
109+
result = 31 * result + port.hashCode()
110+
result = 31 * result + username.hashCode()
111+
result = 31 * result + password.hashCode()
112+
result = 31 * result + privateKey.hashCode()
113+
result = 31 * result + connectionPoolSize.hashCode()
114+
return result
115+
}
87116
}

0 commit comments

Comments
 (0)