View Javadoc
1   /*
2    * Copyright (c) 2021-2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
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 <https://www.gnu.org/licenses/>.
16   */
17  
18  package de.kaiserpfalzedv.commons.api.store;
19  
20  import de.kaiserpfalzedv.commons.api.resources.Resource;
21  
22  import java.util.Optional;
23  import java.util.UUID;
24  
25  /**
26   * StoreService -- A generic store service definition for persistent TOMB resources.
27   *
28   * @param <T> The resource type to be stored.
29   */
30  public interface StoreService<T extends Resource<?>> {
31      /**
32       * @param nameSpace the namespace of the object to load.
33       * @param name      the name of the object to load.
34       * @return the object or an empty {@link Optional}.
35       */
36      Optional<T> findByNameSpaceAndName(final String nameSpace, final String name);
37  
38      /**
39       * @param uid The uid of the data set to load.
40       * @return the object or an empty {@link Optional}.
41       */
42      Optional<T> findByUid(final UUID uid);
43  
44      /**
45       * Persists the given Resource. If the Resource already is stored and the generations are equal, then the
46       * generation is incremented by 1. If the generation of the object is less than the generation of the data already
47       * in the store, then the {@link OptimisticLockStoreException} is thrown.
48       * <p>
49       * On the other hand: if the namespace and name matches but uid not, then the object is considered a duplicate and
50       * the persisting of the new object is denied.
51       *
52       * @param object The data to store.
53       * @return The stored data (the generation may be incremented by 1).
54       * @throws OptimisticLockStoreException Thrown if the generation inside the store is higher than the generation of
55       *                                      the object given in the request.
56       * @throws DuplicateStoreException      If name and nameSpace matches but uid not, then an object is considered a
57       *                                      duplicate.
58       */
59      @SuppressWarnings("UnusedReturnValue")
60      T save(final T object) throws OptimisticLockStoreException, DuplicateStoreException;
61  
62      /**
63       * Remove the object.
64       *
65       * @param object the object to be removed.
66       */
67      void remove(final T object);
68  
69      /**
70       * Remove the object specified by nameSpace and name.
71       *
72       * @param nameSpace the namespace of the object to be deleted.
73       * @param name      the name of the object to be deleted.
74       */
75      void remove(final String nameSpace, final String name);
76  
77      /**
78       * Remove the object specified by the uid.
79       *
80       * @param uid the uid of the object to be removed.
81       */
82      void remove(final UUID uid);
83  }