ありがちな github のエラー

開発環境PCを入れ替えたとき、うっかり忘れて以下をエラーを出してしまう。
maven で、git-hub 上のリポジトリにデプロイしようと思ったときに、、、

[ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project XXXXXX :
 Server 'github' not found in settings -> [Help 1]

PCのユーザフォルダの下の .m2 ファルダの下に、
settings.xml を用意しておくのを忘れてた。。。

<settings>
  <servers>
    <server>
      <id>github</id>
      <username>git-hubユーザアカウント名</username>
      <password>パス</password>
    </server>
  </servers>
</settings>

パスは、git-hub 上で生成したセキュリティのトークンパスを記述する。

PCを新しくしたときに、忘れてた設定。。。

Functionの結果をBiConsumerで実行する(3)

2回も投稿した
Functionの結果をBiConsumerで実行する - Oboe吹きプログラマの黙示録
Functionの結果をBiConsumerで実行する(2) - Oboe吹きプログラマの黙示録
だが、まだまだ改良の余地がある。

andThen なる and で連結の書き方をもっと短くできるはずである。
Objects.requireNonNull による NULLチェックなど除去してしまい、
以下のようにする。

import java.util.function.BiConsumer;
import java.util.function.Function;

@FunctionalInterface
public interface ApplyBiConsumer<T, U> {
   void accept(T t, U u);

   static <T, U, V> ApplyBiConsumer<T, U> of(Function<T, V> f, BiConsumer<U, V> b) {
      return (t,u)->{
         b.accept(u, f.apply(t));
      };
   }
   default <V> ApplyBiConsumer<T, U> and(Function<T, V> f, BiConsumer<U, V> b) {
      return (l, r) -> {
         accept(l, r);
         of(f, b).accept(l, r);
      };
   }
}

これなら、and による連結も
サンプル

import lombok.Data;
@Data
public class Foo{
   private String name;
   private int id;
   private LocalDate birthday;
}

import lombok.Data;
@Data
public class Person{
   private String name;
   private int id;
   private LocalDate birthday;
   private String info;
}
Foo foo;       // Foo のインスタンス
Person person; // Person のインスタンス

// Foo の方に値セット済

ApplyBiConsumer.of(Foo::getName, Person::setName)
.and(Foo::getId, Person::setId)
.and(Foo::getBirthday, Person::setBirthday)
.accept(foo, person);

とかなり簡潔に書けるようになる。

Functionの結果をBiConsumerで実行する(2)

昨日書いた、
Functionの結果をBiConsumerで実行する - Oboe吹きプログラマの黙示録
もっとよく考えてみれば、、
ApplyBiConsumer のリストではく、
続けてafterオペレーションを実行する andThen を使うのが良さそうである。

BiConsumer継承の方法では、

import java.util.function.BiConsumer;
import java.util.function.Function;

@FunctionalInterface
public interface ApplyBiConsumer<T, U> extends BiConsumer<T, U>{
   static <T, U, V> ApplyBiConsumer<T, U> of(Function<T, V> f, BiConsumer<U, V> b) {
       return (t,u)->{
          b.accept(u, f.apply(t));
       };
   }
}
import lombok.Data;
@Data
public class Foo{
   private String name;
   private int id;
   private LocalDate birthday;
}

import lombok.Data;
@Data
public class Person{
   private String name;
   private int id;
   private LocalDate birthday;
   private String info;
}

andThen を使って、、、

Foo foo;       // Foo のインスタンス
Person person; // Person のインスタンス

// Foo の方に値セット済

ApplyBiConsumer.of(Foo::getName, Person::setName)
.andThen(ApplyBiConsumer.of(Foo::getId, Person::setId))
.andThen(ApplyBiConsumer.of(Foo::getBirthday, Person::setBirthday))
.accept(foo, person);

BiConsumer継承ではなく、andThen と同じ and と短い名称にすれば、、

import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;

@FunctionalInterface
public interface ApplyBiConsumer<T, U> {
   void accept(T t, U u);

   static <T, U, V> ApplyBiConsumer<T, U> of(Function<T, V> f, BiConsumer<U, V> b) {
       return (t,u)->{
          b.accept(u, f.apply(t));
       };
   }
   default ApplyBiConsumer<T, U> and(ApplyBiConsumer<? super T, ? super U> after) {
      Objects.requireNonNull(after);
      return (l, r) -> {
          accept(l, r);
          after.accept(l, r);
      };
   }
}

継承でない方では、、

Foo foo;       // Foo のインスタンス
Person person; // Person のインスタンス

// Foo の方に値セット済

ApplyBiConsumer.of(Foo::getName, Person::setName)
.and(ApplyBiConsumer.of(Foo::getId, Person::setId))
.and(ApplyBiConsumer.of(Foo::getBirthday, Person::setBirthday))
.accept(foo, person);

最終的には、後日投稿の以下がベスト
Functionの結果をBiConsumerで実行する(3) - Oboe吹きプログラマの黙示録

Functionの結果をBiConsumerで実行する

Webアプリ開発をしているとよく、画面プレゼンテーション層と
サービスロジック層の間の為にコンバータなるものを作らされたりして
面倒なものです。
よく書かされてしまう、getter で持ってきて、setter に食わせる
  a.setPhonNumber(b.getPhoneNumber());
みたいなものを大量に書かされて、まあコンパクトに書けないことはないんですけど、
単純に左から右へ渡すのでなく、加工などの処置が必要で
生成済インスタンスの状態毎に処置が必要な時は、面倒くさいものです。
先日の、new 演算子で生成する変数のコーディングを減らす方法 - Oboe吹きプログラマの黙示録
に触発されて、
名称はさておき、以下を考えました。。。

getterとして、Function<T, R> で取得して、setter として BiConsumer を実行します。

import java.util.function.BiConsumer;
import java.util.function.Function;

@FunctionalInterface
public interface ApplyBiConsumer<T, U>{
   void accept(T t, U u);

   static <T, U, V> ApplyBiConsumer<T, U> of(Function<T, V> f, BiConsumer<U, V> c) {
       return (t,u)->{
          c.accept(u, f.apply(t));
       };
   }
}

使用サンプルは以下のとおりです。

import lombok.Data;
@Data
public class Foo{
   private String name;
   private int id;
   private LocalDate birthday;
}

import lombok.Data;
@Data
public class Person{
   private String name;
   private int id;
   private LocalDate birthday;
   private String info;
}
Foo foo;       // Foo のインスタンス
Person person; // Person のインスタンス
// TODO foo と person のインスタンス生成、

// foo から person へ情報をセット

ApplyBiConsumer.of(Foo::getName, Person::setName).accept(foo, person);
ApplyBiConsumer.of(Foo::getId, Person::setId).accept(foo, person);
ApplyBiConsumer.of(Foo::getBirthday, Person::setBirthday).accept(foo, person);

// 型が違うものをセットする例
ApplyBiConsumer.of(e->((Foo)e).getDay().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))
            , Item::setInfo).accept(foo, person);
            
// もしくは、BiConsumer の方で対処する場合、、
ApplyBiConsumer.of(Foo::getDay, (t, u)->{
   ((Person)t).setInfo(((LocalDate)u).format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
}).accept(foo, person);

a.setPhonNumber(b.getPhoneNumber()); の記述形式の方が短いと言えますが、
ラムダ式で書けるということは、変換式をsetter である BiConsumer の実行まで待てるので、
以下のように、この ApplyBiConsumer をリストに溜めてから実行できます。

List<ApplyBiConsumer<Foo, Person>> alist = Arrays.asList(
   ApplyBiConsumer.of(Foo::getName, Person::setName),
   ApplyBiConsumer.of(Foo::getId, Person::setId),
   ApplyBiConsumer.of(Foo::getBirthday, Person::setBirthday)
);

を用意しておけば、(Java11なら、List.of() で書くと思います。)

alist.stream().forEach(e->e.accept(foo, person));

のように、いつでもセットを実行できるわけです。

でも、この ApplyBiConsumer は、以下のように、BiConsumer 継承したものとして、
定義した方が良いのかもしれません。
継承なら、 void accept(T t, U u); を以下のとおりここでは書かないです。

import java.util.function.BiConsumer;
import java.util.function.Function;

@FunctionalInterface
public interface ApplyBiConsumer<T, U> extends BiConsumer<T, U>{
   static <T, U, V> ApplyBiConsumer<T, U> of(Function<T, V> f, BiConsumer<U, V> b) {
       return (t,u)->{
          b.accept(u, f.apply(t));
       };
   }
}

最終的には、後日投稿の以下がベスト
Functionの結果をBiConsumerで実行する(3) - Oboe吹きプログラマの黙示録

String の null チェック、空チェック

Java11 を使うようになっても、
 org.apache.commons.lang.StringUtils.isEmpty(String)
や、
 org.springframework.util.StringUtils.isEmpty(String)
を外部JARを使って、nullチェックの代わりと "" でないことをチェックする
コードをよく見かけるという哀れな状況がある。
しかも、org.springframework.util.StringUtils.isEmpty の方は、
@Deprecated なはずだ、それでも springframework StringUtils を使うなら、
むしろ hasText(CharSequence) の方を使って null なら false であることを
チェックすべきだ。

String が空であるかのチェック、"" だけでなく、" " や、全角空白文字のみであるか、
改行文字、"\n", "\r\n" だけであるか、同時に、isEmpty() の代わりに null チェックも
する場合、、

Java11 なら、String の isBlank() を使うのが最も良い。

同等の機能を、Java8 でも処理したければ、
 commons.lang なら、String の StringUtils.isBlank(CharSequence)
 springframework なら、StringUtils.hasText(CharSequence)

が妥当であろう。

String をチェックして、null あるいは、空文列、半角空白、全角空白、改行文字、タブ文字
だけで構成する文字列であるかどうかチェックして
if~else 文を書く場合、もちろん if 文 で、str==null || と続けて if 文の式を書いても
良いのだろうが、

Java11 なら、

// String str を判定

Optional.ofNullable(str).map(String::strip)
.filter(Predicate.not(String::isBlank)).ifPresentOrElse(e->{
   // TODO ブランク、空、でない文字列の処理

}, ()->{
   // TODO null または、空の場合の処理

});

Java11 では、trim は、全角空白を削除してくれないが、
strip / stripLeading / stripTrailing は、全角空白を削除してくれる。
から上の方法が成立する。

同じことを Java8 で

Optional.ofNullable(str)
.map(e->e.replaceFirst("^[\\h\\H]+", "").replaceFirst("[\\h\\H]+$", ""))
.<Runnable>map(e->()->{
   // TODO ブランク、空、でない文字列の処理

).orElse(()->{
   // TODO null または、空の場合の処理

}).run();

正規表現
\h : 水平方向の空白文字 [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]
\H : 水平方向以外の空白文字: [^\h]
を利用する。

new 演算子で生成する変数のコーディングを減らす方法

私が作った Fieldsetter 以下は、
https://github.com/yipuran/yipuran-core/blob/master/src/main/java/org/yipuran/util/Fieldsetter.java

https://github.com/yipuran/yipuran-core/wiki#fieldsetter

フィールド名称を指定する点で、タイプセーフではない。
名称を誤ってもコンパイルエラーにはならないからだ。

とは言え、new 演算子コンストラクタで生成したインスタンス変数に、
setter を大量に実行するのも読みやすいかもしれないが、
そういう new で生成してセットするしたものを沢山書くと、
変数が沢山、生まれてしまい、それも嫌だという我儘がある。
つまり、以下のようなものを沢山書きたくない。

Foo foo1 = new Foo();
foo1.setId(1001);
foo1.setName("Yip1");
foo1.setLocate("Tokyo");

Foo foo2 = new Foo();
foo2.setId(1002);
foo2.setName("Yip2");
foo2.setLocate("Yokohama");

考えて次のように、UnaryOperator の代わりに、
Fuction<T, T> を生成する関数型インターフェースを用意する。

import java.util.function.Function;

@FunctionalInterface
public interface ObjectSetter<T, U>{
   void accept(T t, U u);
   public static <T, U> Function<T, T> of(ObjectSetter<T, U> osetter, U u){
      return t->{
         osetter.accept(t, u);
         return t;
      };
   }
}

これを、Optional の map メソッドに渡して最後に、get() でsetter実行済のインスタンス
取得するのだ。

例) ( サンプルなので、ここでの記述量を少なくするために lombok を使用した対象クラス)

import lombok.Data;

@Data
public class Foo{
   private Integer id;
   private String name;
   private String locate;
}

これを、java.util.OptionalObjectSetter で以下のように setter実行済を取得する。

Foo foo = Optional.of(new Foo())
         .map(ObjectSetter.of(Foo::setId, 1001))
         .map(ObjectSetter.of(Foo::setName, "Yip1"))
         .map(ObjectSetter.of(Foo::setLocate, "Tokyo"))
         .get();

わざわざ、こんなことする理由は、new 演算子で生成した変数のsetterメソッドを実行するのを
回避したいだけですが、これは、以下のようにさらに結果を次のメソッドの引数に渡す時など、
よけいな変数宣言をしなくて済む。

List<Foo> list = new ArrayList<>();
//

list.add( Optional.of(new Foo())
          .map(ObjectSetter.of(Foo::setId, 1001))
          .map(ObjectSetter.of(Foo::setName, "Yip1"))
          .map(ObjectSetter.of(Foo::setLocate, "Tokyo"))
          .get() );

結局、この ObjectSetter は、
yipuran-core で、AcceptFunction として、
用意してある。
yipuran-core/AcceptFunction.java at master · yipuran/yipuran-core · GitHub

漢字の正規表現

CJK 統合漢字 を Unicode正規表現

漢字のみである場合の正規表現Java

^[\u4E00-\u9FFF]+$

漢字と分類されるのが微妙な次の文字を含めるならば、、

\u3005 々 // 同上記号・同の字点
\u3006 〆 // 締め
\u3007 〇 // 漢数字のゼロ

^[\u4E00-\u9FFF\u3005-\u3007]+$