]>
Commit | Line | Data |
---|---|---|
8dfb76c9 MG |
1 | package ro.ieval.fonbot; |
2 | ||
3 | import org.eclipse.jdt.annotation.Nullable; | |
4 | ||
5 | /* | |
6 | * Copyright © 2013 Marius Gavrilescu | |
7 | * | |
8 | * This file is part of FonBot. | |
9 | * | |
10 | * FonBot is free software: you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation, either version 3 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * FonBot is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. | |
22 | */ | |
23 | ||
24 | /** | |
25 | * An address to which messages can be sent using {@link Utils#sendMessage(android.content.Context, Address, int)} | |
26 | * | |
27 | * Addresses are defined by a protocol and a data part. The data is a protocol-dependent opaque String. | |
28 | * | |
29 | * @author Marius Gavrilescu <marius@ieval.ro> | |
30 | */ | |
31 | final class Address { | |
32 | /** | |
33 | * The protocol of an Address. | |
34 | * @author Marius Gavrilescu <marius@ieval.ro> | |
35 | */ | |
36 | public static enum Protocol{ | |
37 | /** | |
38 | * The protocol used for sending messages via short text messages. The data for a SMS address is the phone number. | |
39 | */ | |
40 | SMS, | |
41 | /** | |
42 | * The protocol used for sending messages via anything handled by the iEval server. The data is an opaque String supplied by the iEval server. | |
43 | */ | |
44 | HTTP, | |
45 | /** | |
46 | * The protocol used for sending messages to {@link FonBotLocalActivity} | |
47 | */ | |
48 | LOCAL, | |
49 | /** | |
50 | * The protocol used for suppressing messages. | |
51 | */ | |
52 | NULL | |
53 | } | |
54 | ||
55 | /** | |
56 | * An address for suppressing messages | |
57 | */ | |
58 | public static final Address BLACKHOLE=new Address(Utils.toNonNull(Protocol.NULL), null); | |
59 | /** | |
60 | * The protocol part of the Address. | |
61 | */ | |
62 | public final Protocol protocol; | |
63 | /** | |
64 | * The data part of the Address. Can be null | |
65 | */ | |
66 | public final String data; | |
67 | ||
10f7c6fc MG |
68 | /** The ID of this request. Used in annotations. Can be null */ |
69 | public final transient String requestId; | |
70 | ||
8dfb76c9 MG |
71 | /** |
72 | * Construct an Address from its parts | |
73 | * | |
74 | * @param protocol the protocol part of the Address | |
75 | * @param data the data part of the Address | |
76 | */ | |
77 | public Address(final Protocol protocol, final @Nullable String data){ | |
78 | this.protocol=protocol; | |
79 | this.data=data; | |
10f7c6fc MG |
80 | this.requestId=null; |
81 | } | |
82 | ||
83 | /** | |
84 | * Construct an Address from its parts | |
85 | * | |
86 | * @param protocol the protocol part of the Address | |
87 | * @param data the data part of the Address | |
88 | * @param requestId the request ID | |
89 | */ | |
90 | public Address(final Protocol protocol, final @Nullable String data, final String requestId){ | |
91 | this.protocol=protocol; | |
92 | this.data=data; | |
93 | this.requestId=requestId; | |
8dfb76c9 MG |
94 | } |
95 | ||
96 | /** | |
97 | * Construct an Address from its string representation (the protocol and data in this order separated by a single space character). Does the reverse of {@link #toString()} | |
98 | * | |
99 | * @param address the Address string representation | |
100 | * @throws IllegalArgumentException if the protocol part is not a member of the {@link Protocol} enum | |
101 | */ | |
102 | public Address(final String address) throws IllegalArgumentException{ | |
103 | final String[] parts=address.split(" ", 2); | |
104 | this.protocol=Protocol.valueOf(parts[0]); | |
105 | this.data=parts[1]; | |
10f7c6fc | 106 | this.requestId=null; |
8dfb76c9 MG |
107 | } |
108 | ||
109 | /** | |
110 | * Returns the string representation of an Address, which is the protocol and data in this order separated by a single space character | |
111 | */ | |
112 | @Override | |
113 | public String toString(){ | |
114 | return protocol+" "+data; | |
115 | } | |
116 | } |