7b481519999b608173985af40fa6a1065751672a
[Sone.git] / src / main / java / net / pterodactylus / sone / database / Database.java
1 /*
2  * Sone - Database.java - Copyright © 2011 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.database;
19
20 import java.util.Collection;
21
22 import net.pterodactylus.sone.data.Sone;
23
24 /**
25  * Interface for Sone’s database.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public interface Database {
30
31         /**
32          * Returns whether the given Sone is a local Sone.
33          *
34          * @param sone
35          *            The Sone to check
36          * @return {@code true} if the given Sone is a local Sone, {@code false}
37          *         otherwise
38          * @throws IllegalArgumentException
39          *             if {@code sone} is {@code null}
40          * @throws DatabaseException
41          *             if a database error occurs
42          */
43         public boolean isLocalSone(Sone sone) throws DatabaseException;
44
45         /**
46          * Returns whether the given Sone ID belongs to a local Sone.
47          *
48          * @param id
49          *            The Sone ID to check
50          * @return {@code true} if the given Sone ID belongs to a local Sone,
51          *         {@code false} otherwise
52          * @throws IllegalArgumentException
53          *             if {@code id} is {@code null}
54          * @throws DatabaseException
55          *             if a database error occurs
56          */
57         public boolean isLocalSone(String id) throws DatabaseException;
58
59         /**
60          * Returns whether the given Sone is a remote Sone.
61          *
62          * @param sone
63          *            The Sone to check
64          * @return {@code true} if the given Sone is a remote Sone, {@code false}
65          *         otherwise
66          * @throws IllegalArgumentException
67          *             if {@code sone} is {@code null}
68          * @throws DatabaseException
69          *             if a database error occurs
70          */
71         public boolean isRemoteSone(Sone sone) throws DatabaseException;
72
73         /**
74          * Returns whether the given Sone ID belongs to a remote Sone.
75          *
76          * @param id
77          *            The Sone ID to check
78          * @return {@code true} if the given Sone ID belongs to a remote Sone,
79          *         {@code false} otherwise
80          * @throws IllegalArgumentException
81          *             if {@code id} is {@code null}
82          * @throws DatabaseException
83          *             if a database error occurs
84          */
85         public boolean isRemoteSone(String id) throws DatabaseException;
86
87         /**
88          * Returns the Sone with the given ID, creating a new Sone if a Sone with
89          * the given ID does not exist and {@code create} is {@code true}. When
90          * searching for a Sone with the given IDs, local Sones are preferred.
91          *
92          * @param id
93          *            The ID of the new Sone
94          * @param create
95          *            {@code true} to create a new Sone if a Sone with the given ID
96          *            does not exist, {@code false} to return {@code null} if a Sone
97          *            with the given ID does not exist
98          * @return The created Sone
99          * @throws IllegalArgumentException
100          *             if {@code id} is {@code null}
101          * @throws DatabaseException
102          *             if a database error occurs
103          */
104         public Sone getSone(String id, boolean create) throws DatabaseException;
105
106         /**
107          * Returns all known Sones.
108          *
109          * @return All known Sones
110          * @throws DatabaseException
111          *             if a database error occurs
112          */
113         public Collection<Sone> getSones() throws DatabaseException;
114
115         /**
116          * Returns all known local Sones.
117          *
118          * @return All known local Sones
119          * @throws DatabaseException
120          *             if a database error occurs
121          */
122         public Collection<Sone> getLocalSones() throws DatabaseException;
123
124         /**
125          * Returns all known remote Sones.
126          *
127          * @return All known remote Sones
128          * @throws DatabaseException
129          *             if a database error occurs
130          */
131         public Collection<Sone> getRemoteSones() throws DatabaseException;
132
133         /**
134          * Returns the local Sone with the given ID, creating it if it doesn’t exist
135          * and {@code create} is {@code true}.
136          *
137          * @param id
138          *            The ID of the Sone
139          * @param create
140          *            {@code true} to create a Sone if no Sone with the given ID
141          *            exists yet, {@code false} to return {@code null}
142          * @return The existing Sone, the created Sone, or {@code null}
143          * @throws DatabaseException
144          *             if adatabase error occurs
145          */
146         public Sone getLocalSone(String id, boolean create) throws DatabaseException;
147
148         /**
149          * Returns the remote Sone with the given ID, creating it if it doesn’t
150          * exist and {@code create} is {@code true}.
151          *
152          * @param id
153          *            The ID of the Sone
154          * @param create
155          *            {@code true} to create a Sone if no Sone with the given ID
156          *            exists yet, {@code false} to return {@code null}
157          * @return The existing Sone, the created Sone, or {@code null}
158          * @throws DatabaseException
159          *             if adatabase error occurs
160          */
161         public Sone getRemoteSone(String id, boolean create) throws DatabaseException;
162
163         /**
164          * Stores the given Sone. The given Sone has to be an object that was
165          * returned by a previous call to {@link #getSone(String, boolean)}.
166          *
167          * @param sone
168          *            The Sone to store
169          * @throws IllegalArgumentException
170          *             if {@code sone} is {@code null}, or the Sone was not
171          *             retrieved by a call to {@link #getSone(String, boolean)}
172          * @throws DatabaseException
173          *             if a database error occurs
174          */
175         public void saveSone(Sone sone) throws DatabaseException;
176
177         /**
178          * Removes the given Sone from the database.
179          *
180          * @param sone
181          *            The Sone to remove
182          * @throws DatabaseException
183          *             if a database error occurs
184          */
185         public void removeSone(Sone sone) throws DatabaseException;
186
187         /**
188          * Removes the Sone with the given ID from the database.
189          *
190          * @param id
191          *            The ID of the Sone to remove
192          * @throws DatabaseException
193          *             if a database error occurs
194          */
195         public void removeSone(String id) throws DatabaseException;
196
197 }