Skip to content

Commit

Permalink
feat: add ListUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-Guillemin committed Sep 26, 2024
1 parent d76cb8f commit c676e15
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

public class Constants {

public static final String PROPERTIES_TO_JSON_DELIMITER = "\", \"";
public static final String PROPERTIES_TO_JSON_PREFIX = "[ \"";
public static final String PROPERTIES_TO_JSON_SUFFIX = "\" ]";
public static final String JSON_ARRAY_DELIMITER = "\", \"";
public static final String JSON_ARRAY_PREFIX = "[ \"";
public static final String JSON_ARRAY_SUFFIX = "\" ]";

public static final String SPRING_PROFILE_DEVELOPMENT = "dev";
public static final String SPRING_PROFILE_PRODUCTION = "prod";
public static final String SPRING_PROFILE_TEST = "test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package fr.recia.collabsoft.configuration.bean;

import fr.recia.collabsoft.util.ListUtil;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

import static fr.recia.collabsoft.configuration.Constants.PROPERTIES_TO_JSON_DELIMITER;
import static fr.recia.collabsoft.configuration.Constants.PROPERTIES_TO_JSON_PREFIX;
import static fr.recia.collabsoft.configuration.Constants.PROPERTIES_TO_JSON_SUFFIX;
import static fr.recia.collabsoft.configuration.Constants.JSON_ARRAY_DELIMITER;
import static fr.recia.collabsoft.configuration.Constants.JSON_ARRAY_PREFIX;
import static fr.recia.collabsoft.configuration.Constants.JSON_ARRAY_SUFFIX;

@Data
public class CorsProperties {
Expand All @@ -40,18 +40,10 @@ public String toString() {
return "\"CorsProperties\": {" +
"\n\t\"enable\": " + enable +
",\n\t\"allowCredentials\": " + allowCredentials +
",\n\t\"allowedOrigins\": " + allowedOrigins.stream()
.map(String::valueOf)
.collect(Collectors.joining(PROPERTIES_TO_JSON_DELIMITER, PROPERTIES_TO_JSON_PREFIX, PROPERTIES_TO_JSON_SUFFIX)) +
",\n\t\"exposedHeaders\": " + exposedHeaders.stream()
.map(String::valueOf)
.collect(Collectors.joining(PROPERTIES_TO_JSON_DELIMITER, PROPERTIES_TO_JSON_PREFIX, PROPERTIES_TO_JSON_SUFFIX)) +
",\n\t\"allowedHeaders\": " + allowedHeaders.stream()
.map(String::valueOf)
.collect(Collectors.joining(PROPERTIES_TO_JSON_DELIMITER, PROPERTIES_TO_JSON_PREFIX, PROPERTIES_TO_JSON_SUFFIX)) +
",\n\t\"allowedMethods\": " + allowedMethods.stream()
.map(String::valueOf)
.collect(Collectors.joining(PROPERTIES_TO_JSON_DELIMITER, PROPERTIES_TO_JSON_PREFIX, PROPERTIES_TO_JSON_SUFFIX)) +
",\n\t\"allowedOrigins\": " + ListUtil.toStringList(allowedOrigins, JSON_ARRAY_DELIMITER, JSON_ARRAY_PREFIX, JSON_ARRAY_SUFFIX) +
",\n\t\"exposedHeaders\": " + ListUtil.toStringList(exposedHeaders, JSON_ARRAY_DELIMITER, JSON_ARRAY_PREFIX, JSON_ARRAY_SUFFIX) +
",\n\t\"allowedHeaders\": " + ListUtil.toStringList(allowedHeaders, JSON_ARRAY_DELIMITER, JSON_ARRAY_PREFIX, JSON_ARRAY_SUFFIX) +
",\n\t\"allowedMethods\": " + ListUtil.toStringList(allowedMethods, JSON_ARRAY_DELIMITER, JSON_ARRAY_PREFIX, JSON_ARRAY_SUFFIX) +
"\n}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import fr.recia.collabsoft.db.entity.User;
import fr.recia.collabsoft.db.repository.AssociatedAppRepository;
import fr.recia.collabsoft.db.repository.FileRepository;
import fr.recia.collabsoft.web.interceptor.bean.SoffitHolder;
import fr.recia.collabsoft.model.enums.Authority;
import fr.recia.collabsoft.model.pojo.JsonFileBody;
import fr.recia.collabsoft.web.interceptor.bean.SoffitHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import fr.recia.collabsoft.db.entity.QMetadata;
import fr.recia.collabsoft.db.entity.User;
import fr.recia.collabsoft.db.repository.MetadataRepository;
import fr.recia.collabsoft.web.interceptor.bean.SoffitHolder;
import fr.recia.collabsoft.model.enums.Authority;
import fr.recia.collabsoft.model.pojo.JsonMetadataBody;
import fr.recia.collabsoft.web.interceptor.bean.SoffitHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/fr/recia/collabsoft/util/ListUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 GIP-RECIA, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package fr.recia.collabsoft.util;

import java.util.List;
import java.util.stream.Collectors;

public class ListUtil {

public static <T> String toStringList(List<T> list) {
return toStringList(list, ", ");
}

public static <T> String toStringList(List<T> list, String delimiter) {
return toStringList(list, delimiter, "[ ", " ]");
}

public static <T> String toStringList(List<T> list, String delimiter, String prefix, String suffix) {
return list.stream()
.map(String::valueOf)
.collect(Collectors.joining(delimiter, prefix, suffix));
}

}

0 comments on commit c676e15

Please sign in to comment.