コーディング規約

私達は Google’s Java Style Guidelines に、いくつかの追加と変更を加えたものを使用しており、以下に記載しています。

ちなみに

You can use our code styles for Eclipse or IntelliJ IDEA to let your IDE format the code correctly for you. Visit Sponge Code Styles.

  • 改行

    • Unixの改行コード(\n) を使用します。

      • WindowsのGitユーザは git config --global core.autocrlf true で自動で変換するように設定できる

  • 1行の文字数

    • Javadocは80

    • コードは150

    • 可読性を向上させるためならば、自由に改行してよい

  • インデント

    • インデントには2スペースではなく、4スペースを使用する

  • 縦方向の空白

    • クラス、インターフェース、列挙子など、初めに出てくるメンバの前 (例: class Example { の後ろ) と、最後のメンバの後ろには空行を入れる

  • ファイルヘッダ

    • File headers must contain the license headers for the project. Use the updateLicenses Gradle task to add them automatically

  • インポート

    • インポートは以下の順に行い、それぞれのグループは空行で区切られる

      • 静的インポート

      • その他すべてのインポート

      • java imports

      • javax のインポート

    • 最上位パッケージでインポートがグループ化されていないという点で、これは Google のスタイル異なります。彼らはすべてをまとめてグループ化します。

  • 例外

    • 例外を無視する場合は、例外の変数名を ignored にする

  • フィールドアクセス

    • Qualify all field accesses with this

  • Javadocs

    • @author を使用しない

    • 段落は <p></p> で囲む

    • 「1節」の最初の文字を大文字にし、ピリオドはつけない (例: @param name Player to affect)

  • End of file

    • Each file should end with an empty line

コーディング規約

  • APIで null を返す代わりに、 Optional を使用する

  • メソッドのパラメータに null を許可する場合は(javax.* の) @Nullable アノテーションをつける必要がある。すべてのメソッドのパラメータはデフォルトで @NotNull アノテーションをつける。

  • Use Google Preconditions for null- and argument checking.

Gist

かなり長い文章が2つありますが、Google の Java 規約を読むことをお勧めします。すぐに開始するためには、以下の適切にフォーマットされた例を参照してください。

/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.example;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.inject.Inject;
import org.slf4j.Logger;

import java.util.Optional;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import javax.annotation.Nullable;

/**
* An example class which generates a new ID based on a specified base string
* and a randomly generated integer.
*
* <p>There is a chance the integer purposely fails to generate, in which case
* you can choose to provide a backup integer.</p>
*/
public class Example {

    private static final long SEED = 4815162342L;

    @Inject
    private Logger logger;

    private final String base;
    private final Random random;

    public Example(String base) {
        checkNotNull(base, "The specified base string cannot be null!");
        this.base = base;
        this.random = ThreadLocalRandom.current();
        this.random.setSeed(SEED);
    }

    /**
    * Generates and returns an ID using the base string specified on creation
    * or the alternative string if specified as well as a randomly generated
    * integer, which purposely fails to generate around 50% of the time.
    *
    * <p>A {@link ThreadLocalRandom} is used to check if the integer should
    * be generated and generates the integer itself if so.</p>
    *
    * @param alternate An alternate base string which will be used if not null
    * @return The generated ID, if available
    */
    public Optional<String> generateId(@Nullable String alternate) {
        if (this.random.nextBoolean()) {
            return Optional.of(alternate == null ? this.base : alternate + " - " + this.random.nextInt());
        }

        return Optional.empty();
    }

    /**
    * Generates and returns an ID using the base string specified on creation,
    * using a randomly generated integer if it was generated successfully, or
    * using the backup integer you specify.
    *
    * <p>A {@link ThreadLocalRandom} is used to check if the integer should
    * be generated and generates the integer itself if so. If it was not
    * generated, that is when your backup integer will be used.</p>
    *
    * @param backup A backup integer to use to create the ID with
    * @return The generated ID using the generated integer or the ID created
    *     using the backup integer specified
    */
    public String generateId(int backup) {
        return generateId(null).orElse(this.base + " - " + backup);
    }

}