]> git.basschouten.com Git - openhab-addons.git/blob
12d3c1d2ef91394f6ee68d7417bae4b4166ad52f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.icalendar.internal.logic;
14
15 import java.time.Instant;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * A single event.
24  *
25  * @author Michael Wodniok - Initial contribution
26  * @author Andrew Fiddian-Green - Added support for event description
27  */
28 @NonNullByDefault
29 public class Event implements Comparable<Event> {
30     public final List<CommandTag> commandTags = new ArrayList<CommandTag>();
31     public final Instant end;
32     public final Instant start;
33     public final String title;
34
35     public Event(String title, Instant start, Instant end, String description) {
36         this.title = title;
37         this.start = start;
38         this.end = end;
39
40         if (description.isEmpty()) {
41             return;
42         }
43
44         String[] lines = description.replace("<p>", "").replace("</p>", "\n").split("\n");
45         for (String line : lines) {
46             CommandTag tag = CommandTag.createCommandTag(line);
47             if (tag != null) {
48                 commandTags.add(tag);
49             }
50         }
51     }
52
53     @Override
54     public String toString() {
55         String[] tagStrings = new String[this.commandTags.size()];
56         for (int i = 0; i < tagStrings.length; i++) {
57             tagStrings[i] = this.commandTags.get(i).toString();
58         }
59         return "Event(title: " + this.title + ", start: " + this.start.toString() + ", end: " + this.end.toString()
60                 + ", commandTags: List(" + String.join(", ", tagStrings) + ")";
61     }
62
63     @Override
64     public boolean equals(@Nullable Object other) {
65         if (other == null || other.getClass() != this.getClass()) {
66             return false;
67         }
68         final Event otherEvent = (Event) other;
69         return (this.title.equals(otherEvent.title) && this.start.equals(otherEvent.start)
70                 && this.end.equals(otherEvent.end));
71     }
72
73     @Override
74     public int compareTo(Event o) {
75         return start.compareTo(o.start);
76     }
77 }