59fc0d74ccf6f055b21965c0e72a435712e3a4da
[fonbot.git] / src / ro / ieval / fonbot / Address.java
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
68 /**
69 * Construct an Address from its parts
70 *
71 * @param protocol the protocol part of the Address
72 * @param data the data part of the Address
73 */
74 public Address(final Protocol protocol, final @Nullable String data){
75 this.protocol=protocol;
76 this.data=data;
77 }
78
79 /**
80 * 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()}
81 *
82 * @param address the Address string representation
83 * @throws IllegalArgumentException if the protocol part is not a member of the {@link Protocol} enum
84 */
85 public Address(final String address) throws IllegalArgumentException{
86 final String[] parts=address.split(" ", 2);
87 this.protocol=Protocol.valueOf(parts[0]);
88 this.data=parts[1];
89 }
90
91 /**
92 * Returns the string representation of an Address, which is the protocol and data in this order separated by a single space character
93 */
94 @Override
95 public String toString(){
96 return protocol+" "+data;
97 }
98 }
This page took 0.024405 seconds and 3 git commands to generate.