μ μ ν©ν 리 λ©μλ
μ μ (static), ν©ν 리(factory), λ©μλ(method)
GoFλΌλ λμμΈ ν¨ν΄ μ€ ν©ν 리 ν¨ν΄μμ μ λν λ¨μ΄λ‘ κ°μ²΄λ₯Ό μμ±νλ μν μ λΆλ¦¬νκ² λ€λ κ²μ λ§νλ€.
(νμ§λ§ μ μ ν©ν 리 λ©μλμ ν©ν 리 ν¨ν΄μ λͺ ννκ² λ€λ₯΄κ³ , λμμΈ ν¨ν΄ μ€μ μ΄μ μΌμΉνλ ν¨ν΄μ΄ μλ€κ³ νλ€. μ΄ λΆλΆμ λν΄μ 곡λΆλ₯Ό ν΄λ΄μΌκ² λ€..)
μ’ λ μ½κ² μ€λͺ νμλ©΄ μμ±μλ₯Ό ν΅ν΄ κ°μ²΄λ₯Ό μμ±νλκ² μλλΌ λ³λ λ©μλλ₯Ό μ μν΄μ κ°μ²΄λ₯Ό μμ±ν μ μλλ‘ κ°μ²΄ λ΄μ μ μ λ©μλλ₯Ό μ μν΄λμ κ²!
μ νν μ΄λ€ λ©μλλ₯Ό μκΈ°νλ κ±΄μ§ μμλ₯Ό λ€μ΄λ³΄κ² λ€.
java.time ν¨ν€μ§μ ν¬ν¨λ LocalDateTime ν΄λμ€μ μ μ ν©ν 리 λ©μλλ₯Ό μ΄ν΄ 보면 μμ±μλ₯Ό ν΅ν΄ κ°μ²΄λ₯Ό μμ±νλ κ²μ΄ μλλΌ of λ©μλλ₯Ό μ¬μ©νκ³ , of λ©μλ λ΄λΆμ κ°μ²΄λ₯Ό μμ±νλ μ½λκ° μλ κ²μ λ³Ό μ μλ€.
LocalDateTime ldt = LocalDateTime.of(2022, 1, 1, 1, 30, 0);
public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) {
LocalDate date = LocalDate.of(year, month, dayOfMonth);
LocalTime time = LocalTime.of(hour, minute, second);
return new LocalDateTime(date, time);
}
κ·Έλ¦¬κ³ java.math ν¨ν€μ§μ ν¬ν¨λ BigIntegerν΄λμ€μ κ²½μ°λ valueOf, smallPrime, largePrime λ±μ λ©μλλ₯Ό μ 곡νλλ° ν΄λΉ λ©μλλ₯Ό μ΄ν΄λ³΄λ©΄ λ©μλ λ΄λΆμμ κ°μ²΄λ₯Ό μμ±νκ³ μλ€.
public static BigInteger valueOf(long val) {
// If -MAX_CONSTANT < val < MAX_CONSTANT, return stashed constant
if (val == 0)
return ZERO;
if (val > 0 && val <= MAX_CONSTANT)
return posConst[(int) val];
else if (val < 0 && val >= -MAX_CONSTANT)
return negConst[(int) -val];
return new BigInteger(val);
}
private static BigInteger smallPrime(int bitLength, int certainty, Random rnd) {
int magLen = (bitLength + 31) >>> 5;
int temp[] = new int[magLen];
int highBit = 1 << ((bitLength+31) & 0x1f); // High bit of high int
int highMask = (highBit << 1) - 1; // Bits to keep in high int
while (true) {
// Construct a candidate
for (int i=0; i < magLen; i++)
temp[i] = rnd.nextInt();
temp[0] = (temp[0] & highMask) | highBit; // Ensure exact length
if (bitLength > 2)
temp[magLen-1] |= 1; // Make odd if bitlen > 2
BigInteger p = new BigInteger(temp, 1);
// Do cheap "pre-test" if applicable
if (bitLength > 6) {
long r = p.remainder(SMALL_PRIME_PRODUCT).longValue();
if ((r%3==0) || (r%5==0) || (r%7==0) || (r%11==0) ||
(r%13==0) || (r%17==0) || (r%19==0) || (r%23==0) ||
(r%29==0) || (r%31==0) || (r%37==0) || (r%41==0))
continue; // Candidate is composite; try another
}
// All candidates of bitLength 2 and 3 are prime by this point
if (bitLength < 4)
return p;
// Do expensive test if we survive pre-test (or it's inapplicable)
if (p.primeToCertainty(certainty, rnd))
return p;
}
}
μ΄λ κ² μ μ ν©ν 리 λ©μλκ° λ¬΄μμΈμ§ μκ² λλ©΄ μμ±μκ° μλλ° μ μ μ ν©ν 리 λ©μλλ₯Ό μ¬μ©ν΄μΌ νλ κ±΄μ§ μλ¬Έμ΄ μκΈ΄λ€! 'μμ±μ λμ μ μ ν©ν 리 λ©μλλ₯Ό κ³ λ €νλΌ'λΌλ λ§λ μκ³ ..
μμ±μμ μ΄λ€ μ°¨μ΄κ° μκ³ , μ μ ν©ν 리 λ©μλμ μ₯μ μ΄ λ¬΄μμΈμ§λ λ€μ ν¬μ€ν μμ~!
μ λ΄μ© ν¬μ€ν μμ± μλ£!
λκΈ