Skip to content

Commit

Permalink
refactor: 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed Dec 4, 2024
1 parent dc139bc commit e93e5d5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.common.collect.Sets;
import com.livk.auth.server.common.constant.SecurityConstants;
import com.livk.auth.server.common.token.OAuth2BaseAuthenticationToken;
import com.livk.commons.util.BaseStreamUtils;
import com.livk.commons.util.StreamUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -80,7 +80,7 @@ default Authentication convert(HttpServletRequest request) {
OAuth2ErrorCodes.INVALID_CLIENT, SecurityConstants.ACCESS_TOKEN_REQUEST_ERROR_URI)));

// 扩展信息
Map<String, Object> additionalParameters = BaseStreamUtils.convert(request.getParameterNames())
Map<String, Object> additionalParameters = StreamUtils.convert(request.getParameterNames())
.filter(Predicate.isEqual(OAuth2ParameterNames.GRANT_TYPE)
.negate()
.and(Predicate.isEqual(OAuth2ParameterNames.SCOPE).negate()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.livk.commons;

import com.livk.auto.service.annotation.SpringAutoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationNotAllowedException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
Expand Down Expand Up @@ -57,7 +57,6 @@
*
* @author livk
*/
@Slf4j
@SpringAutoService
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component(SpringContextHolder.BEAN_NAME)
Expand Down Expand Up @@ -230,7 +229,7 @@ else if (applicationContext instanceof GenericApplicationContext context) {
registerBean(context, beanDefinition, beanName);
}
else {
log.error("bean register fail name: {} instantClass: {}", beanName, beanDefinition.getResolvableType());
throw new BeanCreationNotAllowedException(beanName, "missing created bean factory");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
*
* @param <T> the type parameter
* @author livk
* @see BaseStreamUtils#convert(Enumeration)
* @see StreamUtils#convert(Enumeration)
*/
final class EnumerationSpliterator<T> extends Spliterators.AbstractSpliterator<T> {

private final Enumeration<T> enumeration;

private EnumerationSpliterator(Enumeration<T> enumeration, int additionalCharacteristics) {
super(Long.MAX_VALUE, additionalCharacteristics);
private EnumerationSpliterator(Enumeration<T> enumeration) {
super(Long.MAX_VALUE, Spliterator.ORDERED);
this.enumeration = enumeration;
}

Expand All @@ -47,7 +47,7 @@ private EnumerationSpliterator(Enumeration<T> enumeration, int additionalCharact
* @return spliterator
*/
public static <T> Spliterator<T> spliteratorUnknownSize(Enumeration<T> enumeration) {
return new EnumerationSpliterator<>(Objects.requireNonNull(enumeration), Spliterator.ORDERED);
return new EnumerationSpliterator<>(Objects.requireNonNull(enumeration));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.livk.commons.util;

import lombok.experimental.UtilityClass;

import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
Expand All @@ -35,7 +37,6 @@
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import lombok.experimental.UtilityClass;

/**
* <p>
Expand All @@ -45,7 +46,7 @@
* @author livk
*/
@UtilityClass
public class BaseStreamUtils {
public class StreamUtils {

/**
* 合并Map,key相同的则合并成List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* <p>
Expand Down Expand Up @@ -106,8 +104,13 @@ public HttpHeaders headers(HttpServletRequest request) {
* @return attributes
*/
public Map<String, Object> attributes(HttpServletRequest request) {
return BaseStreamUtils.convert(request.getAttributeNames())
.collect(Collectors.toMap(Function.identity(), request::getAttribute));
Enumeration<String> attributeNames = request.getAttributeNames();
Map<String, Object> attributes = Maps.newHashMap();
while (attributeNames.hasMoreElements()) {
String attributeName = attributeNames.nextElement();
attributes.put(attributeName, request.getAttribute(attributeName));
}
return attributes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
*
* @author livk
*/
class BaseStreamUtilsTest {
class StreamUtilsTest {

@Test
void testConcatMap() {
Map<String, List<String>> result1 = BaseStreamUtils.concat(Map.of("username", "livk", "password", "123456"),
Map<String, List<String>> result1 = StreamUtils.concat(Map.of("username", "livk", "password", "123456"),
Map.of("username", "root", "host", "192.168.1.1"));
Map<String, List<String>> listMap1 = Map.of("username", List.of("livk", "root"), "password", List.of("123456"),
"host", List.of("192.168.1.1"));
assertEquals(listMap1, result1);

Map<String, List<String>> result2 = BaseStreamUtils.concat(Map.of("username", "livk", "password", "123456"),
Map<String, List<String>> result2 = StreamUtils.concat(Map.of("username", "livk", "password", "123456"),
Map.of("username", "root", "host", "192.168.1.1"), Map.of("username", "admin", "host", "192.168.1.2"));
Map<String, List<String>> listMap2 = Map.of("username", List.of("livk", "root", "admin"), "password",
List.of("123456"), "host", List.of("192.168.1.1", "192.168.1.2"));
Expand All @@ -57,30 +57,30 @@ void testConcatMap() {

@Test
void testConcatArray() {
int[] result1 = BaseStreamUtils.concat(new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 });
int[] result1 = StreamUtils.concat(new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 });
int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
assertArrayEquals(intArray, result1);

long[] result2 = BaseStreamUtils.concat(new long[] { 1, 2, 3 }, new long[] { 4, 5, 6 }, new long[] { 7, 8, 9 });
long[] result2 = StreamUtils.concat(new long[] { 1, 2, 3 }, new long[] { 4, 5, 6 }, new long[] { 7, 8, 9 });
long[] longArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
assertArrayEquals(longArray, result2);

double[] result3 = BaseStreamUtils.concat(new double[] { 1, 2, 3 }, new double[] { 4, 5, 6 },
double[] result3 = StreamUtils.concat(new double[] { 1, 2, 3 }, new double[] { 4, 5, 6 },
new double[] { 7, 8, 9 });
double[] doubleArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
assertArrayEquals(doubleArray, result3);

String[] result4 = BaseStreamUtils.concat(false, new String[] { "1", "2", "3" }, new String[] { "4", "5", "6" },
String[] result4 = StreamUtils.concat(false, new String[] { "1", "2", "3" }, new String[] { "4", "5", "6" },
new String[] { "1", "2", "3" });
String[] stringArray = { "1", "2", "3", "4", "5", "6", "1", "2", "3" };
assertArrayEquals(stringArray, result4);

String[] result5 = BaseStreamUtils.concatDistinct(new String[] { "1", "2", "3" },
new String[] { "4", "5", "6" }, new String[] { "1", "2", "3" });
String[] result5 = StreamUtils.concatDistinct(new String[] { "1", "2", "3" }, new String[] { "4", "5", "6" },
new String[] { "1", "2", "3" });
String[] stringArrayDistinct = { "1", "2", "3", "4", "5", "6" };
assertArrayEquals(stringArrayDistinct, result5);

Recode[] result6 = BaseStreamUtils
Recode[] result6 = StreamUtils
.concat(new Recode[] { Recode.of("root"), Recode.of("admin") }, new Recode[] { Recode.of("guest") })
.toArray(Recode[]::new);
Recode[] recodeArray = { Recode.of("root"), Recode.of("admin"), Recode.of("guest") };
Expand All @@ -90,12 +90,12 @@ void testConcatArray() {
@Test
void testDistinct() {
List<Integer> list = Stream.of(1, 1, 2, 2, 3, 3, 4, 4)
.filter(BaseStreamUtils.distinct(Function.identity()))
.filter(StreamUtils.distinct(Function.identity()))
.toList();
assertEquals(list, List.of(1, 2, 3, 4));

List<User> users = Stream.of(new User(1, "1"), new User(1, "11"), new User(2, "2"), new User(2, "2"))
.filter(BaseStreamUtils.distinct(User::id))
.filter(StreamUtils.distinct(User::id))
.toList();
assertEquals(users, List.of(new User(1, "1"), new User(2, "2")));
}
Expand All @@ -104,28 +104,28 @@ void testDistinct() {
void mapWithIndex() {
List<String> list = List.of("root", "livk", "admin");
List<User> users1 = List.of(new User(0, "root"), new User(1, "livk"), new User(2, "admin"));
List<User> result1 = list.stream().map(BaseStreamUtils.mapWithIndex(0, (s, i) -> new User(i, s))).toList();
List<User> result1 = list.stream().map(StreamUtils.mapWithIndex(0, (s, i) -> new User(i, s))).toList();
assertIterableEquals(users1, result1);

List<User> users2 = List.of(new User(10, "root"), new User(11, "livk"), new User(12, "admin"));
List<User> result2 = list.stream().map(BaseStreamUtils.mapWithIndex(10, (s, i) -> new User(i, s))).toList();
List<User> result2 = list.stream().map(StreamUtils.mapWithIndex(10, (s, i) -> new User(i, s))).toList();
assertIterableEquals(users2, result2);
}

@Test
void forEachWithIndex() {
List.of("root", "livk", "admin")
.forEach(BaseStreamUtils.forEachWithIndex(0, (s, i) -> System.out.println("index:" + i + " data:" + s)));
.forEach(StreamUtils.forEachWithIndex(0, (s, i) -> System.out.println("index:" + i + " data:" + s)));

List.of("root", "livk", "admin")
.forEach(BaseStreamUtils.forEachWithIndex(10, (s, i) -> System.out.println("index:" + i + " data:" + s)));
.forEach(StreamUtils.forEachWithIndex(10, (s, i) -> System.out.println("index:" + i + " data:" + s)));
}

@Test
void zip() {
Stream<String> result = Stream.of("1", "2", "3", "4", "5", "6");
Stream<String> zip = BaseStreamUtils.zip(stream -> stream.map(Objects::toString), Stream.of(1, 2),
Stream.of(3, 4), Stream.of(5, 6));
Stream<String> zip = StreamUtils.zip(stream -> stream.map(Objects::toString), Stream.of(1, 2), Stream.of(3, 4),
Stream.of(5, 6));

assertLinesMatch(result, zip);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.livk.commons.io.ResourceUtils;
import com.livk.commons.util.BaseStreamUtils;
import com.livk.commons.util.StreamUtils;
import com.livk.context.easyexcel.annotation.ResponseExcel;
import com.livk.context.easyexcel.listener.ExcelMapReadListener;
import lombok.experimental.UtilityClass;
Expand Down Expand Up @@ -107,7 +107,7 @@ private void ordinaryWrite(ExcelWriterBuilder builder, Map<String, ? extends Col

private void templateWrite(ExcelWriterBuilder builder, Map<String, ? extends Collection<?>> result) {
try (ExcelWriter writer = builder.build()) {
result.entrySet().forEach(BaseStreamUtils.forEachWithIndex(0, (entry, index) -> {
result.entrySet().forEach(StreamUtils.forEachWithIndex(0, (entry, index) -> {
WriteSheet writeSheet = EasyExcel.writerSheet(index, entry.getKey())
.registerWriteHandler(new SheetWriteHandler() {
@Override
Expand Down

0 comments on commit e93e5d5

Please sign in to comment.