-
Notifications
You must be signed in to change notification settings - Fork 0
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
cb1abda
commit 4ec0b71
Showing
3 changed files
with
99 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.thesalutyt.ftml; | ||
|
||
import org.thesalutyt.ftml.objects.FTMLObject; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
public class Builder { | ||
private final File file; | ||
private final ArrayList<FTMLObject> fileObjects = new ArrayList<>(); | ||
|
||
public Builder(File file) { | ||
if (file == null) { | ||
throw new IllegalArgumentException("File cannot be null"); | ||
} | ||
if (!file.exists()) { | ||
throw new IllegalArgumentException("File does not exist"); | ||
} | ||
if (!file.isFile()) { | ||
throw new IllegalArgumentException("File is not a file"); | ||
} | ||
|
||
this.file = file; | ||
} | ||
|
||
public Builder addObject(FTMLObject object) { | ||
fileObjects.add(object); | ||
return this; | ||
} | ||
|
||
public Builder dump() { | ||
FTMLObject[] objects = new FTMLObject[fileObjects.size()]; | ||
fileObjects.toArray(objects); | ||
try { | ||
File file = this.file; | ||
FileWriter writer = new FileWriter(file); | ||
|
||
for (FTMLObject object : objects) { | ||
writer.write(object.toString() + "\n"); | ||
} | ||
|
||
writer.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public boolean delete() { | ||
return this.file.delete(); | ||
} | ||
|
||
public FTMLObject[] getObjects() { | ||
return fileObjects.toArray(new FTMLObject[0]); | ||
} | ||
|
||
public Builder get() { | ||
return this; | ||
} | ||
|
||
public File getFile() { | ||
return file; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/thesalutyt/ftml/converter/ObjectConverter.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,31 @@ | ||
package org.thesalutyt.ftml.converter; | ||
|
||
import org.thesalutyt.ftml.objects.FTMLObject; | ||
import org.thesalutyt.ftml.objects.ListObject; | ||
import org.thesalutyt.ftml.objects.ObjectType; | ||
|
||
import java.util.List; | ||
|
||
public class ObjectConverter { | ||
public static FTMLObject toFtml(Object object, String name) { | ||
if (object instanceof String) { | ||
return new FTMLObject(name, object.toString(), ObjectType.String); | ||
} else if (object instanceof Integer) { | ||
return new FTMLObject(name, Integer.parseInt(object.toString()), ObjectType.Integer); | ||
} else if (object instanceof Double) { | ||
return new FTMLObject(name, Double.parseDouble(object.toString()), ObjectType.Double); | ||
} else if (object instanceof Float) { | ||
return new FTMLObject(name, Float.parseFloat(object.toString()), ObjectType.Float); | ||
} else if (object instanceof Boolean) { | ||
return new FTMLObject(name, Boolean.parseBoolean(object.toString()), ObjectType.Boolean); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static ListObject toFtmlList(Object object, String name) { | ||
if (!(object instanceof List<?> list)) throw new IllegalArgumentException(); | ||
|
||
return ListObject.of(new FTMLObject(name, list.toString(), ObjectType.List)); | ||
} | ||
} |
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