Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

How to join nested collections with Stream API

For example, in PushOperation.java in EGit, PushOperationSpecification contains LinkedHashMap<URIish, Collection<RemoteRefUpdate>>. So you can join all remote name with Stream API like:

String specs = this.specification.getURIs().stream()
        .map(uri -> this.specification.getRefUpdates(uri))
        .collect(StringBuilder::new,
                (sb, refupdates) -> sb.append(refupdates.stream()
                        .map(remote -> remote.getRemoteName())
                        .collect(Collectors.joining(","))),
                (sb1, sb2) -> sb1.append(sb2))
        .toString();

You cannot wrap checked exceptions with lambda in Java

Context

  • You can use lambda as lazy evaluation syntax in Java.

Problem

You can wrap RuntimeException with lambda like (it shows "omit exception"):

import java.util.function.Supplier;

public class OmitRuntimeException {
    public static void main(String[] args) {
        OmitRuntimeException o = new OmitRuntimeException();
        o.wrapLambda(() -> o.throwException());
    }

    public void wrapLambda(Supplier<String> msg) {
        try {
            System.out.println(msg.get());
        } catch (Exception e) {
            System.out.println("omit exception");
        }
    }

    public String throwException() throws RuntimeException {
        if (true) {
            throw new RuntimeException();
        }
        return "";
    }
}

But you cannot wrap ordinal Exception with lambda like:

import java.util.function.Supplier;

public class OmitException {
    public static void main(String[] args) {
        OmitException o = new OmitException();
        o.wrapLambda(() -> o.throwException());
    }

    public void wrapLambda(Supplier<String> msg) {
        try {
            System.out.println(msg.get());
        } catch (Exception e) {
            System.out.println("omit exception");
        }
    }

    public String throwException() throws Exception {
        if (true) {
            throw new Exception();
        }
        return "";
    }
}

And you will get a compile error:

$ LANG=C javac OmitException.java 
OmitException.java:6: error: unreported exception Exception; must be caught or declared to be thrown
        o.wrapLambda(() -> o.throwException());
                                           ^
1 error

Solution:

Use RuntimeException, or wrap the function that thows checked exceptions with another function.

Run a command with specific user priviledge in Windows (like sudo in Unix)

Problem:

You can use runas command to run a command with specific user privilege. (Note: You have to type password into command prompt):

C:\>runas /user:domain\username cmd
Enter the password for domain\username:
Attempting to start cmd as user "domain\username" ...

But, you will get error with a command with arguments:

C:\>runas /user:domain\username net start WebClient

RUNAS USAGE:

RUNAS [ [/noprofile | /profile] [/env] [/savecred | /netonly] ]
        /user:<UserName> program

You can also use Start-Process -Verb runas with the same aim without typing password (UAC dialog will pop up for privileges):

PS > Start-Process -Verb runas cmd

But you will get an error when you want pass arguments to the command:

PS > Start-Process -Verb runas net start WebClient

Start-Process : A positional parameter cannot be found that accepts argument 'WebClient'.
At line:1 char:1
+ Start-Process <<<<  -Verb runas net start WebClient
    + CategoryInfo          : InvalidArgument: (:) [Start-Process]、ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

Or you will get another error when you quote the command and arguments:

PS > Start-Process -Verb runas "net start WebClient"
Start-Process : This command cannot be executed due to the error: The system ca
nnot find the file specified.
At line:1 char:14
+ Start-Process <<<<  -Verb runas "net start WebClient"
    + CategoryInfo          : InvalidOperation: (:) [Start-Process]、InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Solution:

You should quote the command and arguments in runas command:

C:\>runas /user:domain\username "net start WebClient"
Enter the password for domain\username:
Attempting to start net start WebClient as user "domain\username" ...

You should use -ArgumentList option to pass argument in Start-Process cmdlet:

Start-Process -Verb runas -ArgumentList "stop WebClient" net

You cannot have two forks from one repository in GitHub

Problem:

  • “Fork” button in GitHub forks a repository into your account.
  • You cannot rename the repository when you fork it.
  • You cannot have two repositories with identical name.

So, you cannot have two forks from one repository simultaneously.

Solution:

There is no workaround. If you want to make two forks, you have to make two GitHub account.

ISO/IEC 12207 (JIS X 0160:2012) ソフトウェア構成管理プロセスのうちバージョン管理システム、GitHub/GitLab/etc、git-flow/GitHub Flowでカバーできる内容

(In English: What can you do for ISO/IEC 12207 (JIS X 0160:2012) Software Configuration Management Process with VCSs, GitHub/GitLab/etc, and git-flow/GitHub Flow)

ISO/IEC 12207 (JIS X 0160:2012)では、ソフトウェア開発におけるソフトウェア構成管理プロセスが規定されている。その中の作業には、バージョン管理システムを使えばカバーできるもの、GitHub/GitLab/etcを使っていればカバーできるもの、git-flow/GitHub Flowでカバーできるものがある。 ISO/IEC 12207 (JIS X 0160:2012)のソフトウェア構成管理の各項目に対する、各技術要素のカバー範囲を表にまとめてみた。

見出し バージョン管理システムでできること GitHab/GitLab/etcでできること git-flow/GitHub Flowでできること git-flowではできないこと 備考
7.2.2 - - - - -
7.2.2.1 ソフトウェア品目の利用
ソフトウェア品目の完全性の維持
ソフトウェア品目の完全性の確立
7.2.2.2 - - - - -
7.2.2.2 a) ソフトウェア構成管理戦略の作成 git-flowをそのままソフトウェア構成管理戦略と規定する想定
7.2.2.2 b) 生成される品目のベースライン化 生成される品目の識別
生成される品目の定義
ベースライン化に必要なレビュー等が作業フローに含まれるものとする
7.2.2.2 c) 品目の修正の制御
品目のリリースの制御
7.2.2.2 d) 修正の利用
リリースの利用
7.2.2.2 e) 品目及び修正の状態の記録 品目及び修正の状態の報告
7.2.2.2 f) 品目の完全性の確保
品目の一貫性の確保
7.2.2.2 g) 品目の保管の制御 品目の取り扱いの制御 品目の納入の制御
7.2.2.3 - - - - -
7.2.2.3.1 - - - - -
7.2.2.3.1.1 構成管理アクティビティの文書化
構成管理アクティビティを遂行するための手順の文書化
構成管理アクティビティを遂行するためのスケジュールの文書化
構成管理アクティビティを遂行する責任を負う組織の文書化
ソフトウェア開発,保守などの他の組織との関係の文書化
git-flowをそのまま文書として使用する想定
7.2.2.3.2 - - - - -
7.2.2.3.2.1 版の参照番号の識別 ベースラインを確立する文書の識別
その他の識別の詳細
7.2.2.3.3 - - - - -
7.2.3.3.1 変更依頼の識別及び記録
修正されたソフトウェア品目の実装,検証及びリリース各修正の追跡用監査証跡
修正されたソフトウェア品目の修正の承認の追跡用監査証跡
構成制御されたソフトウェア品目に対するアクセスの制御
変更の分析及び評価
変更依頼の承認又は不承認
修正されたソフトウェア品目の修正理由の追跡用監査証跡
構成制御されたソフトウェア品目に対するアクセスの監査
7.2.2.3.4 - - - - -
7.2.2.3.4.1 ソフトウェア品目の状態及び履歴を示す管理記録
プロジェクトにおける変更回数の記録
リリースの識別子の生成
リリースの回数の記録
リリース間の比較
最新のソフトウェア品目の版 状態報告書の準備
ベースラインの準備
7.2.2.3.5 - - - - -
7.2.2.3.5.1 要求事項に対するソフトウェア品目の機能的な完全さ及びソフトウェア品目の物理的な完全さの決定と保証
7.2.2.3.6 - - - - -
7.2.2.3.6.1 ソフトウェア製品及び文書のリリース及び納入の管理 コード及び文書の原本の保守
安全性又はセキュリティの重大な機能を含んだコード及び文書の関係する組織の方針に従った保管と納入