forked from Prunoideae/ProbeJS-Forge
-
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
Showing
2 changed files
with
50 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.probejs.document.type; | ||
|
||
import com.probejs.util.StringUtil; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* {@code {value: number, mapping: Map<string,string>, [x in string]: string}} | ||
* @author ZZZank | ||
*/ | ||
@AllArgsConstructor | ||
public class TypeObject implements DocType { | ||
|
||
public static boolean test(String type) { | ||
return type.startsWith("{") && type.endsWith("}"); | ||
} | ||
|
||
private final Map<String, DocType> raw; | ||
|
||
public TypeObject(String typeStr) { | ||
if (!test(typeStr)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.raw = new HashMap<>(); | ||
StringUtil.splitLayer(typeStr, ",") | ||
.stream() | ||
.map(String::trim) | ||
.map(s -> StringUtil.splitFirst(s, ":")) | ||
.forEach(p -> raw.put(p.first().trim(), DocTypeResolver.resolve(p.second()))); | ||
} | ||
|
||
@Override | ||
public String getTypeName() { | ||
return transform(dummyTransformer); | ||
} | ||
|
||
@Override | ||
public String transform(BiFunction<DocType, String, String> transformer) { | ||
return "{" + raw.entrySet() | ||
.stream() | ||
.map(entry -> String.format("%s: %s", entry.getKey(), entry.getValue().transform(transformer))) | ||
.collect(Collectors.joining(", ")) + "}"; | ||
} | ||
} |