-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ca7ee9
commit cbc107e
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/com/napzak/domain/genre/api/dto/request/cursor/OldestCursor.java
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,45 @@ | ||
package com.napzak.domain.genre.api.dto.request.cursor; | ||
|
||
import com.napzak.global.common.exception.NapzakException; | ||
import com.napzak.global.common.exception.code.ErrorCode; | ||
|
||
public class OldestCursor { | ||
private final Long id; | ||
|
||
public OldestCursor(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
// O-{id} | ||
@Override | ||
public String toString() { | ||
final StringBuilder stringBuilder = new StringBuilder(); | ||
return stringBuilder | ||
.append("O-") | ||
.append(id) | ||
.toString(); | ||
} | ||
|
||
public static OldestCursor fromString(String cursor) { | ||
if (!cursor.startsWith("O-")) { | ||
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT); | ||
} | ||
String[] split = cursor.split("-"); | ||
|
||
if (split.length != 2) { | ||
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT); | ||
} | ||
final long id; | ||
|
||
try{ | ||
id = Long.parseLong(split[1]); | ||
} catch (NumberFormatException e) { | ||
throw new NapzakException(ErrorCode.INVALID_CURSOR_FORMAT); | ||
} | ||
return new OldestCursor(id); | ||
} | ||
} |