-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from yumemi-inc/feature/apply_api_guideline
既存コードをAPI Guidelineに準拠させる
- Loading branch information
Showing
13 changed files
with
567 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// ControllableGenerator.swift | ||
// | ||
// | ||
// Created by 古宮 伸久 on 2022/04/08. | ||
// | ||
|
||
import Foundation | ||
|
||
/// 制御可能な乱数生成器です。 | ||
struct ControllableGenerator { | ||
|
||
/// 唯一のインスタンスを生成します。 | ||
/// | ||
/// このイニシャライザーは、複数回呼び出しても意味がありません。 | ||
/// 乱数生成方法の都合で、同じ振る舞いをするインスタンスになります。 | ||
private init() { | ||
} | ||
} | ||
|
||
extension ControllableGenerator { | ||
|
||
/// 唯一の、制御可能な乱数生成器インスタンスです。 | ||
static var shared = ControllableGenerator() | ||
|
||
/// 乱数生成時に使用するシード値を `seed` でリセットします。 | ||
/// - Parameter seed: 新たに使用するシード値です。 | ||
static func reset(withSeed seed: Int) { | ||
srand48(seed) | ||
} | ||
|
||
/// 乱数生成時に使用するシード値を `area` と `date` から算出してリセットします。 | ||
/// - Parameters: | ||
/// - area: シード値の算出に使う、地域情報 | ||
/// - date: シード値の算出に使う、日付情報 | ||
static func resetUsing(area: Area, date: Date) { | ||
|
||
var hasher = Hasher() | ||
|
||
hasher.combine(area) | ||
hasher.combine(date) | ||
|
||
let seed = hasher.finalize() | ||
|
||
reset(withSeed: seed) | ||
} | ||
} | ||
|
||
extension ControllableGenerator : RandomNumberGenerator { | ||
|
||
/// 次の乱数を生成します。 | ||
/// - Returns: 次の乱数です。 | ||
func next() -> UInt64 { | ||
UInt64(drand48() * Double(UInt64.max)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// YumemiWeather.APIQuality.swift | ||
// | ||
// | ||
// Created by Tomohiro Kumagai on 2024/05/02 | ||
// | ||
// | ||
|
||
extension YumemiWeather { | ||
|
||
/// API の品質を表現します。 | ||
enum APIQuality { | ||
case sometimesFails(probability: Double) | ||
case alwaysFails | ||
case neverFails | ||
} | ||
} | ||
|
||
extension YumemiWeather { | ||
|
||
/// API の想定品質です。 | ||
static var apiQuality: APIQuality = .sometimesFails(probability: 0.25) | ||
|
||
static func whetherHit(with probability: Double) -> Bool { | ||
return (0 ..< probability).contains(.random(in: 0 ..< 1)) | ||
} | ||
|
||
/// この場所に不安定要素を埋め込みます。 | ||
/// | ||
/// 不安定さの度合いは `apiQuality` に依存します。 | ||
/// - Throws: YumemiError ここで失敗が生成されると YumemiWeatherError.unknownError が送出されます。 | ||
static func introduceInstability() throws { | ||
|
||
switch apiQuality { | ||
|
||
case .neverFails: | ||
return | ||
|
||
case .sometimesFails(let probability) where !whetherHit(with: probability): | ||
return | ||
|
||
case .sometimesFails, .alwaysFails: | ||
throw YumemiWeatherError.unknownError | ||
} | ||
} | ||
|
||
/// この場所に不安定要素を埋め込みます。 | ||
/// | ||
/// 不安定さの度合いは `apiQuality` に依存します。 | ||
/// - Throws: YumemiError ここで失敗が生成されると YumemiWeatherError.unknownError が送出されます。 | ||
func introduceInstability() throws { | ||
try Self.introduceInstability() | ||
} | ||
} |
Oops, something went wrong.