|
| 1 | +/* |
| 2 | + * Copyright 2021-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package icu.easyj.core.factory; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +import javax.annotation.Nonnull; |
| 23 | +import javax.annotation.Nullable; |
| 24 | + |
| 25 | +import icu.easyj.core.util.CollectionUtils; |
| 26 | +import icu.easyj.core.util.StringUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * code相同的服务组 |
| 30 | + * |
| 31 | + * @param <S> 服务类型 |
| 32 | + * @author wangliang181230 |
| 33 | + * @since 0.8.0 |
| 34 | + */ |
| 35 | +@SuppressWarnings("unused") |
| 36 | +public class ServiceGroup<S> { |
| 37 | + |
| 38 | + @Nonnull |
| 39 | + private final String code; |
| 40 | + |
| 41 | + /** |
| 42 | + * 已排好序的 服务列表 |
| 43 | + */ |
| 44 | + @Nonnull |
| 45 | + private final List<ServiceInfo<S>> sortedServiceList; |
| 46 | + |
| 47 | + /** |
| 48 | + * 构造方法 |
| 49 | + */ |
| 50 | + public ServiceGroup(String code) { |
| 51 | + if (StringUtils.isBlank(code)) { |
| 52 | + throw new IllegalArgumentException("code 不能为空"); |
| 53 | + } |
| 54 | + |
| 55 | + this.code = code; |
| 56 | + this.sortedServiceList = new ArrayList<>(); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + // get -------------------------------------------------------------------------- |
| 61 | + |
| 62 | + @Nullable |
| 63 | + public ServiceInfo<S> getFirstInfo() { |
| 64 | + if (CollectionUtils.isNotEmpty(this.sortedServiceList)) { |
| 65 | + return this.sortedServiceList.get(0); |
| 66 | + } else { |
| 67 | + return null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Nullable |
| 72 | + public S getFirst() { |
| 73 | + if (CollectionUtils.isNotEmpty(this.sortedServiceList)) { |
| 74 | + return this.sortedServiceList.get(0).getService(); |
| 75 | + } else { |
| 76 | + return null; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public List<ServiceInfo<S>> getAllInfo() { |
| 81 | + return new ArrayList<>(this.sortedServiceList); |
| 82 | + } |
| 83 | + |
| 84 | + public List<S> getAllService() { |
| 85 | + List<S> serviceList = new ArrayList<>(); |
| 86 | + for (ServiceInfo<S> serviceInfo : this.sortedServiceList) { |
| 87 | + serviceList.add(serviceInfo.getService()); |
| 88 | + } |
| 89 | + return serviceList; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + // add -------------------------------------------------------------------------- |
| 94 | + |
| 95 | + public void add(ServiceInfo<S> serviceInfo) { |
| 96 | + this.checkServiceInfo(serviceInfo); |
| 97 | + |
| 98 | + this.sortedServiceList.add(serviceInfo); |
| 99 | + this.sort(); |
| 100 | + } |
| 101 | + |
| 102 | + public void addAll(List<ServiceInfo<S>> serviceInfoList) { |
| 103 | + if (CollectionUtils.isEmpty(serviceInfoList)) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + serviceInfoList.forEach(this::checkServiceInfo); |
| 108 | + |
| 109 | + this.sortedServiceList.addAll(serviceInfoList); |
| 110 | + this.sort(); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + // Getter --------------------------------------------------------------- |
| 115 | + |
| 116 | + public String getCode() { |
| 117 | + return code; |
| 118 | + } |
| 119 | + |
| 120 | + public boolean isDefaultGroup() { |
| 121 | + return ServiceFactory.DEFAULT_SERVICE_CODE.equals(this.code); |
| 122 | + } |
| 123 | + |
| 124 | + public List<ServiceInfo<S>> getOriginalList() { |
| 125 | + return this.sortedServiceList; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + // Private --------------------------------------------------------------- |
| 130 | + |
| 131 | + private void checkServiceInfo(ServiceInfo<S> serviceInfo) { |
| 132 | + if (ServiceFactory.DEFAULT_SERVICE_CODE.equals(this.code) && serviceInfo.isDefault()) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + if (!Objects.equals(this.code, serviceInfo.getCode())) { |
| 137 | + throw new IllegalArgumentException("serviceInfo 的 code 与 ServiceGroup 的 code 不一致"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * 按 order、code、className 排序 |
| 143 | + */ |
| 144 | + private void sort() { |
| 145 | + this.sortedServiceList.sort((a, b) -> { |
| 146 | + int ret = a.getOrder() - b.getOrder(); |
| 147 | + if (ret == 0) { |
| 148 | + ret = a.getCode().compareTo(b.getCode()); |
| 149 | + if (ret == 0) { |
| 150 | + ret = a.getService().getClass().getName().compareTo(b.getService().getClass().getName()); |
| 151 | + } |
| 152 | + } else { |
| 153 | + if (ret < 0) { |
| 154 | + ret = -1; |
| 155 | + } else { |
| 156 | + ret = 1; |
| 157 | + } |
| 158 | + } |
| 159 | + return ret; |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + @Override |
| 165 | + public String toString() { |
| 166 | + if (this.sortedServiceList.isEmpty()) { |
| 167 | + return this.code + "[]"; |
| 168 | + } |
| 169 | + |
| 170 | + StringBuilder sb = new StringBuilder(); |
| 171 | + |
| 172 | + sb.append(this.code).append('['); |
| 173 | + |
| 174 | + for (ServiceInfo<S> serviceInfo : this.sortedServiceList) { |
| 175 | + if (sb.length() > 0) { |
| 176 | + sb.append(','); |
| 177 | + } |
| 178 | + sb.append(serviceInfo); |
| 179 | + } |
| 180 | + |
| 181 | + sb.append(']'); |
| 182 | + |
| 183 | + return sb.toString(); |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments