LoginSignup
2
0

More than 1 year has passed since last update.

Java

Last updated at Posted at 2022-06-28

Java

stream

  • distinctByKeys
    /**
     * リストから重複するオブジェクトを除却する関数を取得する
     *
     * @param keyExtractors 重複判定に使用するフィールドのgetter
     * @param <T>
     * @return 重複除却関数
     */
    private static <T> Predicate<T> distinctByKeys( Function<? super T, ?>... keyExtractors ) {
        final Map<List<?>, Boolean> seen = new ConcurrentHashMap<>();

        return t -> {
            final List<?> keys = Arrays.stream( keyExtractors )
                    .map( ke -> ke.apply( t ) )
                    .collect( Collectors.toList() );

            return Objects.isNull( seen.putIfAbsent( keys, Boolean.TRUE ) );
        };
    }

            List<HogeDto> distinctDtos = hogeList.stream()
                    .filter( distinctByKeys(
                            HogeDto::getName,
                            HogeDto::getAge ) )
                    .collect( Collectors.toList() );
  • map to map

ファイル操作

Spring

jpa

spring security

UT

controller

@InitBinder

  • エンドポイントごとに分けたい場合, 例えばカスタムバリデータなど
    • クラス名のキャメルケースで指定すればいい、webfluxだとMonoが必要?
      • WebDataBinderのobjectNameプロパティで判定している模様
      @InitBinder("updateUserRequestMono")
      public void initBinder( WebDataBinder binder ) {
          binder.addValidators( this.requestValidator );
      }
    
      public Mono<ResponseEntity<Void>> updateUser( String organizationSearchId, String branchOfficeSearchId,
              String sessionId, Mono<UpdateUserRequest> updateUserRequest,
              ServerWebExchange exchange ) {
      }
    

相関チェック

いくつか方法あり

相関チェックのためにValidatorをimpleした場合

  • @NotNullをかけているフィールドでも、validateメソッド内ではnullが入ってこれるのでnullチェック必要

tips

値の詰め替えはmapperを介すのが良さそう

  • 変換パターンが増えて困る
  • シリアライズの実装が必要になる?

内部クラスの利用

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Hoge {

    @Required
    @Valid
    private List<Fuga> fugas;

    private Double hoge;

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class Fuga {

        @Required
        private String uuid;

        @Required
        private Double fuga;
    }
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0