|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + * |
| 14 | + * Copyright 2014 Edgar Espina |
| 15 | + */ |
| 16 | +package io.jooby; |
| 17 | + |
| 18 | +import javax.annotation.Nonnull; |
| 19 | +import javax.annotation.Nullable; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +/** |
| 23 | + * Default registry which use a simply key/value mechanism for storing and retrieving services. |
| 24 | + * |
| 25 | + * @author edgar |
| 26 | + * @since 2.0.0 |
| 27 | + */ |
| 28 | +public interface ServiceRegistry extends Registry { |
| 29 | + /** |
| 30 | + * Registered service keys. |
| 31 | + * |
| 32 | + * @return Service keys. |
| 33 | + */ |
| 34 | + @Nonnull Set<ServiceKey<?>> keySet(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Retrieve a service/resource by key. |
| 38 | + * |
| 39 | + * @param key Service/resource key. |
| 40 | + * @param <T> Service/resource type. |
| 41 | + * @return Service. |
| 42 | + * @throws RegistryException If there was a runtime failure while providing an instance. |
| 43 | + */ |
| 44 | + default @Nonnull <T> T get(@Nonnull ServiceKey<T> key) { |
| 45 | + T service = getOrNull(key); |
| 46 | + if (service == null) { |
| 47 | + throw new RegistryException("Service not found: " + key); |
| 48 | + } |
| 49 | + return service; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Retrieve a service/resource by key. |
| 54 | + * |
| 55 | + * @param type Service/resource key. |
| 56 | + * @param <T> Service/resource type. |
| 57 | + * @return Service. |
| 58 | + * @throws RegistryException If there was a runtime failure while providing an instance. |
| 59 | + */ |
| 60 | + default @Nonnull <T> T get(@Nonnull Class<T> type) { |
| 61 | + return get(ServiceKey.key(type)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Retrieve an existing service or <code>null</code> if not exists. |
| 66 | + * |
| 67 | + * @param key Service/resource key. |
| 68 | + * @param <T> Service/resource type. |
| 69 | + * @return Service or <code>null</code>. |
| 70 | + */ |
| 71 | + @Nullable <T> T getOrNull(@Nonnull ServiceKey<T> key); |
| 72 | + |
| 73 | + /** |
| 74 | + * Retrieve an existing service or <code>null</code> if not exists. |
| 75 | + * |
| 76 | + * @param type Service/resource key. |
| 77 | + * @param <T> Service/resource type. |
| 78 | + * @return Service or <code>null</code>. |
| 79 | + */ |
| 80 | + default @Nullable <T> T getOrNull(@Nonnull Class<T> type) { |
| 81 | + return getOrNull(ServiceKey.key(type)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Put a service in this registry. This method overrides any previous registered service. |
| 86 | + * |
| 87 | + * @param key Service/resource key. |
| 88 | + * @param service Service instance. |
| 89 | + * @param <T> Service type. |
| 90 | + * @return Previously registered service or <code>null</code>. |
| 91 | + */ |
| 92 | + @Nullable <T> T put(@Nonnull ServiceKey<T> key, T service); |
| 93 | + |
| 94 | + /** |
| 95 | + * Put a service in this registry. This method overrides any previous registered service. |
| 96 | + * |
| 97 | + * @param type Service/resource key. |
| 98 | + * @param service Service instance. |
| 99 | + * @param <T> Service type. |
| 100 | + * @return Previously registered service or <code>null</code>. |
| 101 | + */ |
| 102 | + default @Nullable <T> T put(@Nonnull Class<T> type, T service) { |
| 103 | + return put(ServiceKey.key(type), service); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Put/register a service in this registry if there isn't the same service already registered. |
| 108 | + * |
| 109 | + * |
| 110 | + * @param key Service/resource key. |
| 111 | + * @param service Service instance. |
| 112 | + * @param <T> Service type. |
| 113 | + * @return Previously registered service or <code>null</code>. |
| 114 | + */ |
| 115 | + @Nullable <T> T putIfAbsent(@Nonnull ServiceKey<T> key, T service); |
| 116 | + |
| 117 | + /** |
| 118 | + * Put/register a service in this registry if there isn't the same service already registered. |
| 119 | + * |
| 120 | + * |
| 121 | + * @param type Service/resource key. |
| 122 | + * @param service Service instance. |
| 123 | + * @param <T> Service type. |
| 124 | + * @return Previously registered service or <code>null</code>. |
| 125 | + */ |
| 126 | + default @Nullable <T> T putIfAbsent(@Nonnull Class<T> type, T service) { |
| 127 | + return putIfAbsent(ServiceKey.key(type), service); |
| 128 | + } |
| 129 | + |
| 130 | + default @Nonnull @Override <T> T require(@Nonnull Class<T> type) { |
| 131 | + return get(ServiceKey.key(type)); |
| 132 | + } |
| 133 | + |
| 134 | + default @Nonnull @Override <T> T require(@Nonnull Class<T> type, @Nonnull String name) { |
| 135 | + return get(ServiceKey.key(type, name)); |
| 136 | + } |
| 137 | +} |
0 commit comments