aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/model
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2024-08-03 20:52:00 -0600
committerIván Ávalos <avalos@disroot.org>2024-08-03 20:52:00 -0600
commit55f6d92c09a5b1d8566b53633d07be3d31010d3b (patch)
treeb86bed801b0bbadb72c7e839296dae1d28b12bbb /src/main/java/org/traccar/model
parentc04ad7d48331253c095fc123ded1b00f6ff871d3 (diff)
parent2788174193def918a3a1a5be3bbed24c9613323f (diff)
downloadtrackermap-server-55f6d92c09a5b1d8566b53633d07be3d31010d3b.tar.gz
trackermap-server-55f6d92c09a5b1d8566b53633d07be3d31010d3b.tar.bz2
trackermap-server-55f6d92c09a5b1d8566b53633d07be3d31010d3b.zip
Merge tag 'tags/v6.3'
Diffstat (limited to 'src/main/java/org/traccar/model')
-rw-r--r--src/main/java/org/traccar/model/Calendar.java24
-rw-r--r--src/main/java/org/traccar/model/ExtendedModel.java16
-rw-r--r--src/main/java/org/traccar/model/Pair.java56
-rw-r--r--src/main/java/org/traccar/model/Typed.java36
-rw-r--r--src/main/java/org/traccar/model/User.java15
5 files changed, 49 insertions, 98 deletions
diff --git a/src/main/java/org/traccar/model/Calendar.java b/src/main/java/org/traccar/model/Calendar.java
index 76c9a2cc1..c1a570037 100644
--- a/src/main/java/org/traccar/model/Calendar.java
+++ b/src/main/java/org/traccar/model/Calendar.java
@@ -19,9 +19,6 @@ package org.traccar.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.data.ParserException;
-import net.fortuna.ical4j.filter.Filter;
-import net.fortuna.ical4j.filter.predicate.PeriodRule;
-import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Period;
import net.fortuna.ical4j.model.component.CalendarComponent;
import net.fortuna.ical4j.model.component.VEvent;
@@ -31,9 +28,8 @@ import org.traccar.storage.StorageName;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.Duration;
-import java.util.Collection;
+import java.time.Instant;
import java.util.Date;
-import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@@ -70,23 +66,19 @@ public class Calendar extends ExtendedModel {
return calendar;
}
- private Collection<VEvent> findEvents(Date date) {
+ public Set<Period<Instant>> findPeriods(Date date) {
if (calendar != null) {
- var filter = new Filter<VEvent>(new PeriodRule<>(new Period(new DateTime(date), Duration.ZERO)));
- return filter.filter(calendar.getComponents(CalendarComponent.VEVENT));
+ var period = new Period<>(date.toInstant(), Duration.ZERO);
+ return calendar.<VEvent>getComponents(CalendarComponent.VEVENT).stream()
+ .flatMap(c -> c.<Instant>calculateRecurrenceSet(period).stream())
+ .collect(Collectors.toUnmodifiableSet());
} else {
- return List.of();
+ return Set.of();
}
}
- public Set<Period> findPeriods(Date date) {
- return findEvents(date).stream()
- .flatMap((e) -> e.calculateRecurrenceSet(new Period(new DateTime(date), Duration.ZERO)).stream())
- .collect(Collectors.toSet());
- }
-
public boolean checkMoment(Date date) {
- return !findEvents(date).isEmpty();
+ return !findPeriods(date).isEmpty();
}
}
diff --git a/src/main/java/org/traccar/model/ExtendedModel.java b/src/main/java/org/traccar/model/ExtendedModel.java
index d5cd094da..f1183e11a 100644
--- a/src/main/java/org/traccar/model/ExtendedModel.java
+++ b/src/main/java/org/traccar/model/ExtendedModel.java
@@ -105,8 +105,8 @@ public class ExtendedModel extends BaseModel {
public double getDouble(String key) {
if (attributes.containsKey(key)) {
Object value = attributes.get(key);
- if (value instanceof Number) {
- return ((Number) attributes.get(key)).doubleValue();
+ if (value instanceof Number numberValue) {
+ return numberValue.doubleValue();
} else {
return Double.parseDouble(value.toString());
}
@@ -118,8 +118,8 @@ public class ExtendedModel extends BaseModel {
public boolean getBoolean(String key) {
if (attributes.containsKey(key)) {
Object value = attributes.get(key);
- if (value instanceof Boolean) {
- return (Boolean) attributes.get(key);
+ if (value instanceof Boolean booleanValue) {
+ return booleanValue;
} else {
return Boolean.parseBoolean(value.toString());
}
@@ -131,8 +131,8 @@ public class ExtendedModel extends BaseModel {
public int getInteger(String key) {
if (attributes.containsKey(key)) {
Object value = attributes.get(key);
- if (value instanceof Number) {
- return ((Number) attributes.get(key)).intValue();
+ if (value instanceof Number numberValue) {
+ return numberValue.intValue();
} else {
return Integer.parseInt(value.toString());
}
@@ -144,8 +144,8 @@ public class ExtendedModel extends BaseModel {
public long getLong(String key) {
if (attributes.containsKey(key)) {
Object value = attributes.get(key);
- if (value instanceof Number) {
- return ((Number) attributes.get(key)).longValue();
+ if (value instanceof Number numberValue) {
+ return numberValue.longValue();
} else {
return Long.parseLong(value.toString());
}
diff --git a/src/main/java/org/traccar/model/Pair.java b/src/main/java/org/traccar/model/Pair.java
index b679de57b..90b82d577 100644
--- a/src/main/java/org/traccar/model/Pair.java
+++ b/src/main/java/org/traccar/model/Pair.java
@@ -1,43 +1,19 @@
+/*
+ * Copyright 2024 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed 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
+ *
+ * http://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.traccar.model;
-import java.util.Objects;
-
-public class Pair<K, V> {
-
- private final K first;
- private final V second;
-
- public Pair(K first, V second) {
- this.first = first;
- this.second = second;
- }
-
- public K getFirst() {
- return first;
- }
-
- public V getSecond() {
- return second;
- }
-
- @SuppressWarnings("rawtypes")
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
-
- Pair pair = (Pair) o;
-
- return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(first, second);
- }
-
+public record Pair<K, V>(K first, V second) {
}
diff --git a/src/main/java/org/traccar/model/Typed.java b/src/main/java/org/traccar/model/Typed.java
index fc671ac70..0476fdc4b 100644
--- a/src/main/java/org/traccar/model/Typed.java
+++ b/src/main/java/org/traccar/model/Typed.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2020 - 2024 Anton Tananaev (anton@traccar.org)
* Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,37 +16,5 @@
*/
package org.traccar.model;
-import java.util.Objects;
-
-public class Typed {
-
- private String type;
-
- public Typed(String type) {
- this.type = type;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- return Objects.equals(type, ((Typed) o).type);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(type);
- }
+public record Typed(String type) {
}
diff --git a/src/main/java/org/traccar/model/User.java b/src/main/java/org/traccar/model/User.java
index 9b8ee3e53..7cf8c25a6 100644
--- a/src/main/java/org/traccar/model/User.java
+++ b/src/main/java/org/traccar/model/User.java
@@ -17,11 +17,13 @@ package org.traccar.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
+import org.apache.commons.lang3.builder.EqualsBuilder;
import org.traccar.storage.QueryIgnore;
import org.traccar.helper.Hashing;
import org.traccar.storage.StorageName;
import java.util.Date;
+import java.util.HashMap;
@StorageName("tc_users")
public class User extends ExtendedModel implements UserRestrictions, Disableable {
@@ -305,4 +307,17 @@ public class User extends ExtendedModel implements UserRestrictions, Disableable
return Hashing.validatePassword(password, hashedPassword, salt);
}
+ public boolean compare(User other, String... exclusions) {
+ if (!EqualsBuilder.reflectionEquals(this, other, "attributes", "hashedPassword", "salt")) {
+ return false;
+ }
+ var thisAttributes = new HashMap<>(getAttributes());
+ var otherAttributes = new HashMap<>(other.getAttributes());
+ for (String exclusion : exclusions) {
+ thisAttributes.remove(exclusion);
+ otherAttributes.remove(exclusion);
+ }
+ return thisAttributes.equals(otherAttributes);
+ }
+
}