Revisão | 434b9f8f428947e276b095478bd0314f525f48ad (tree) |
---|---|
Hora | 2015-11-17 07:40:44 |
Autor | MirrgieRiana |
Commiter | MirrgieRiana |
mirrg.util.hydrogen: update: template
@@ -2,88 +2,142 @@ | ||
2 | 2 | apply plugin: 'eclipse' |
3 | 3 | apply plugin: 'maven' |
4 | 4 | |
5 | -/////////////////////////////////// | |
5 | +////////////////////////////////////////////////////////////////////// | |
6 | 6 | |
7 | +// ★グループId | |
7 | 8 | project.group = 'mirrg' |
9 | + | |
10 | +// ★成果物のID | |
8 | 11 | project.archivesBaseName = 'mirrg.util.hydrogen' |
9 | 12 | |
10 | -version = '1.0' | |
11 | - | |
12 | -dependencies { | |
13 | - compile group: 'commons-collections', name: 'commons-collections', version: '3.2' | |
14 | - testCompile group: 'junit', name: 'junit', version: '4.+' | |
15 | - // version: 'latest.integration' | |
16 | -} | |
17 | - | |
18 | -/////////////////////////////////// | |
19 | - | |
20 | -sourceCompatibility = 1.8 | |
21 | -targetCompatibility = 1.8 | |
22 | - | |
23 | -tasks.withType(AbstractCompile)*.options*.encoding = tasks.withType(GroovyCompile)*.groovyOptions*.encoding = 'UTF-8' | |
24 | - | |
25 | -jar { | |
26 | - manifest { | |
27 | - //attributes 'Main-Class' : 'Main' | |
28 | - attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version | |
29 | - } | |
30 | -} | |
13 | +// ★成果物のバージョン | |
14 | +def superversion = '1.0' // 破壊的なバージョン変更 | |
15 | +def subversion = '0' // 破壊的でないバージョン変更 | |
31 | 16 | |
32 | -task sourcesJar(type: Jar, dependsOn:classes) { | |
33 | - classifier = 'sources' | |
34 | - from sourceSets.main.allSource | |
35 | -} | |
36 | - | |
37 | -task javadocJar(type: Jar, dependsOn:javadoc) { | |
38 | - classifier = 'javadoc' | |
39 | - from javadoc.destinationDir | |
40 | -} | |
41 | - | |
42 | -javadoc { | |
43 | - options.charSet = 'UTF-8' | |
44 | - options.encoding = 'UTF-8' | |
45 | -} | |
17 | +// ★依存関係 | |
18 | +dependencies { | |
46 | 19 | |
47 | -artifacts { | |
48 | - archives sourcesJar | |
49 | - archives javadocJar | |
50 | -} | |
20 | + testCompile group: 'junit', name: 'junit', version: '4.+' | |
51 | 21 | |
52 | -repositories { | |
53 | - mavenCentral() | |
54 | - maven { | |
55 | - url '../../maven' | |
56 | - } | |
57 | - | |
58 | - // ★mirrgリポジトリのデフォルトURL | |
59 | - //maven { | |
60 | - // url 'http://hg.pf.osdn.jp/view/t/ta/tacticsrealize/mirrg.struct.hydrogen/raw-file/tip/maven' | |
61 | - //} | |
22 | + //compile group: 'commons-collections', name: 'commons-collections', version: '3.2' | |
23 | + //compile group: 'mirrg', name: 'mirrg.struct.hydrogen-1.0', version: '+' | |
62 | 24 | |
63 | 25 | } |
64 | 26 | |
65 | -test { | |
66 | - systemProperties 'property': 'value' | |
27 | +// ★追加のリポジトリURL | |
28 | +def mavenRepositories = [ | |
29 | + | |
30 | + // ★mirrgリポジトリのデフォルトURL | |
31 | + //'http://hg.pf.osdn.jp/view/t/ta/tacticsrealize/mirrg.struct.hydrogen/raw-file/tip/maven', | |
32 | + | |
33 | +] | |
34 | + | |
35 | +// ★実行可能Jar | |
36 | +def mainClass = null | |
37 | + | |
38 | +def outputLocation = | |
39 | + | |
40 | + // ★'../../repos'にフォルダ出力する場合 | |
41 | + 'maven' | |
42 | + | |
43 | + // ★'../../maven'にMavenプロジェクトとして出力する場合 | |
44 | + //'dir' | |
45 | + | |
46 | +// ★まとまったJarとして出力する | |
47 | +def allInOne = false | |
48 | + | |
49 | +////////////////////////////////////////////////////////////////////// | |
50 | + | |
51 | +version = superversion + "." + subversion | |
52 | + | |
53 | +// Javaコンパイル設定 | |
54 | + | |
55 | + // ソースのJava準拠指定 | |
56 | + sourceCompatibility = 1.8 | |
57 | + targetCompatibility = 1.8 | |
58 | + | |
59 | + // ソースのエンコーディング指定 | |
60 | + tasks.withType(AbstractCompile)*.options*.encoding = tasks.withType(GroovyCompile)*.groovyOptions*.encoding = 'UTF-8' | |
61 | + | |
62 | +// Meta-Info | |
63 | +jar { | |
64 | + manifest { | |
65 | + | |
66 | + // Mainクラス指定 | |
67 | + if (mainClass != null) attributes 'Main-Class' : mainClass | |
68 | + | |
69 | + attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version | |
70 | + | |
71 | + } | |
72 | +}; | |
73 | + | |
74 | +// ソースとJavadocを成果物に含める | |
75 | + | |
76 | + task sourcesJar(type: Jar, dependsOn:classes) { | |
77 | + classifier = 'sources' | |
78 | + from sourceSets.main.allSource | |
79 | + } | |
80 | + | |
81 | + task javadocJar(type: Jar, dependsOn:javadoc) { | |
82 | + classifier = 'javadoc' | |
83 | + from javadoc.destinationDir | |
84 | + } | |
85 | + | |
86 | + javadoc { | |
87 | + options.charSet = 'UTF-8' | |
88 | + options.encoding = 'UTF-8' | |
89 | + } | |
90 | + | |
91 | + artifacts { | |
92 | + archives sourcesJar | |
93 | + archives javadocJar | |
94 | + } | |
95 | + | |
96 | +// 追加のMavenリポジトリURL設定 | |
97 | +repositories { | |
98 | + mavenCentral() | |
99 | + maven { | |
100 | + url '../../maven' | |
101 | + } | |
102 | + | |
103 | + mavenRepositories.each { | |
104 | + def a = it | |
105 | + maven { | |
106 | + url a | |
107 | + } | |
108 | + } | |
109 | + | |
67 | 110 | } |
68 | 111 | |
69 | -uploadArchives { | |
70 | - repositories { | |
112 | +/* | |
113 | +test { | |
114 | + systemProperties 'property': 'value' | |
115 | +} | |
116 | +*/ | |
71 | 117 | |
72 | - // ★まとまったJarとしてフォルダ出力する場合 | |
73 | - //flatDir { | |
74 | - // dirs 'repos' | |
75 | - //} | |
118 | +// 成果物の出力先 | |
119 | +uploadArchives { | |
120 | + repositories { | |
76 | 121 | |
77 | - // ★mavenプロジェクトとして出力する場合 | |
78 | - mavenDeployer { | |
79 | - pom.artifactId = project.archivesBaseName.replace('.', '-') | |
80 | - repository(url: 'file://localhost/' + file('../../maven').getAbsolutePath()) | |
81 | - } | |
122 | + if (outputLocation == 'dir') { | |
123 | + flatDir { | |
124 | + dirs '../../repos' | |
125 | + } | |
126 | + } | |
82 | 127 | |
83 | - } | |
128 | + if (outputLocation == 'maven') { | |
129 | + mavenDeployer { | |
130 | + pom.artifactId = project.archivesBaseName + "-" + superversion | |
131 | + repository(url: 'file://localhost/' + file('../../maven').getAbsolutePath()) | |
132 | + } | |
133 | + } | |
134 | + | |
135 | + } | |
84 | 136 | } |
85 | 137 | |
86 | -// ★まとまったJarとしてフォルダ出力する場合 | |
87 | -//jar { | |
88 | -// from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } | |
89 | -//} | |
138 | +// 依存Jarを同封 | |
139 | +if (allInOne) { | |
140 | + jar { | |
141 | + from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } | |
142 | + } | |
143 | +} |
@@ -0,0 +1,6 @@ | ||
1 | +package mirrg; | |
2 | + | |
3 | +public class Dummy | |
4 | +{ | |
5 | + | |
6 | +} |
@@ -1,54 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import java.awt.Color; | |
4 | -import java.awt.Graphics2D; | |
5 | -import java.awt.image.BufferedImage; | |
6 | -import java.awt.image.ColorModel; | |
7 | -import java.awt.image.DataBuffer; | |
8 | -import java.awt.image.DataBufferInt; | |
9 | -import java.awt.image.DirectColorModel; | |
10 | -import java.awt.image.Raster; | |
11 | -import java.awt.image.WritableRaster; | |
12 | - | |
13 | -public class HAwt | |
14 | -{ | |
15 | - | |
16 | - /** | |
17 | - * 1ピクセルの枠が付いた文字列を色を指定して描画する。 | |
18 | - */ | |
19 | - public static void drawBoldString(Graphics2D g, String str, int x, int y, Color borderColor, Color fillColor) | |
20 | - { | |
21 | - g.setColor(borderColor); | |
22 | - g.drawString(str, x + 1, y); | |
23 | - g.drawString(str, x - 1, y); | |
24 | - g.drawString(str, x, y + 1); | |
25 | - g.drawString(str, x, y - 1); | |
26 | - g.setColor(fillColor); | |
27 | - g.drawString(str, x, y); | |
28 | - } | |
29 | - | |
30 | - /** | |
31 | - * 0x00RRGGBB・透過なし | |
32 | - */ | |
33 | - public static BufferedImage createBufferedImageFromIntArray(int[] data, int width, int height) | |
34 | - { | |
35 | - DataBuffer dataBuffer = new DataBufferInt(data, width * height); | |
36 | - | |
37 | - WritableRaster raster = Raster.createPackedRaster( | |
38 | - dataBuffer, width, height, width, | |
39 | - new int[] { | |
40 | - 0x00ff0000, 0x0000ff00, 0x000000ff, | |
41 | - }, null); | |
42 | - | |
43 | - //Raster.createPackedRaster(dataBuffer, width, height, 32, new Point()); | |
44 | - //colorModel = new ComponentColorModel( | |
45 | - // ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), false, false, | |
46 | - // Transparency.OPAQUE, DataBuffer.TYPE_INT); | |
47 | - | |
48 | - ColorModel colorModel = new DirectColorModel(24, | |
49 | - 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); | |
50 | - | |
51 | - return new BufferedImage(colorModel, raster, false, null); | |
52 | - } | |
53 | - | |
54 | -} |
@@ -1,110 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import java.awt.Color; | |
4 | - | |
5 | -public class HColor | |
6 | -{ | |
7 | - | |
8 | - /** | |
9 | - * 定義域を外れたdouble型を適切に丸めてColorインスタンスを作成します。 | |
10 | - * rgbaそれぞれ、int値と同じく0以上256未満の値に丸められます。 | |
11 | - */ | |
12 | - public static Color createColor(double r, double g, double b) | |
13 | - { | |
14 | - return createColor(r, g, b, 255); | |
15 | - } | |
16 | - | |
17 | - /** | |
18 | - * 定義域を外れたint型を適切に丸めてColorインスタンスを作成します。 | |
19 | - */ | |
20 | - public static Color createColor(int r, int g, int b) | |
21 | - { | |
22 | - return createColor(r, g, b, 255); | |
23 | - } | |
24 | - | |
25 | - /** | |
26 | - * 定義域を外れたdouble型を適切に丸めてColorインスタンスを作成します。 | |
27 | - * rgbaそれぞれ、int値と同じく0以上256未満の値に丸められます。 | |
28 | - */ | |
29 | - public static Color createColor(double r, double g, double b, double a) | |
30 | - { | |
31 | - return createColor((int) r, (int) g, (int) b, (int) a); | |
32 | - } | |
33 | - | |
34 | - /** | |
35 | - * 定義域を外れたint型を適切に丸めてColorインスタンスを作成します。 | |
36 | - */ | |
37 | - public static Color createColor(int r, int g, int b, int a) | |
38 | - { | |
39 | - if (r < 0) r = 1; | |
40 | - if (g < 0) g = 1; | |
41 | - if (b < 0) b = 1; | |
42 | - if (a < 0) a = 1; | |
43 | - if (r >= 256) r = 255; | |
44 | - if (g >= 256) g = 255; | |
45 | - if (b >= 256) b = 255; | |
46 | - if (a >= 256) a = 255; | |
47 | - return new Color(r, g, b, a); | |
48 | - } | |
49 | - | |
50 | - /** | |
51 | - * ランダムなrgb値と255のa値を持つ色を生成して返します。 | |
52 | - */ | |
53 | - public static Color createRandomColor() | |
54 | - { | |
55 | - return createRandomRangedColor(0, 255); | |
56 | - } | |
57 | - | |
58 | - /** | |
59 | - * 0から127の範囲でランダムなrgb値と255のa値を持つ色を生成して返します。 | |
60 | - */ | |
61 | - public static Color createRandomDarkColor() | |
62 | - { | |
63 | - return createRandomRangedColor(0, 127); | |
64 | - } | |
65 | - | |
66 | - /** | |
67 | - * 128から255の範囲でランダムなrgb値と255のa値を持つ色を生成して返します。 | |
68 | - */ | |
69 | - public static Color createRandomLightColor() | |
70 | - { | |
71 | - return createRandomRangedColor(128, 255); | |
72 | - } | |
73 | - | |
74 | - /** | |
75 | - * minからmaxの範囲でランダムなrgb値と255のa値を持つ色を生成して返します。 | |
76 | - */ | |
77 | - public static Color createRandomRangedColor(int min, int max) | |
78 | - { | |
79 | - return createColor( | |
80 | - HMath.randomBetween(min, max), | |
81 | - HMath.randomBetween(min, max), | |
82 | - HMath.randomBetween(min, max)); | |
83 | - } | |
84 | - | |
85 | - /** | |
86 | - * 2色a, bの中間色を計算します。 | |
87 | - * Aerが0以下の場合はaを返し、Aerが1以上の場合はbを返します。 | |
88 | - * Aerが小数の場合は2色a, bを適切な比率で混色した色を計算して返します。 | |
89 | - * | |
90 | - * @param Aer | |
91 | - * 0以上1未満の小数 | |
92 | - */ | |
93 | - public static Color createLinearRatioColor(double Aer, Color a, Color b) | |
94 | - { | |
95 | - if (Aer <= 0) return b; | |
96 | - if (Aer >= 1) return a; | |
97 | - | |
98 | - return createColor( | |
99 | - Aer * a.getRed() + (1 - Aer) * b.getRed(), | |
100 | - Aer * a.getGreen() + (1 - Aer) * b.getGreen(), | |
101 | - Aer * a.getBlue() + (1 - Aer) * b.getBlue(), | |
102 | - Aer * a.getAlpha() + (1 - Aer) * b.getAlpha()); | |
103 | - } | |
104 | - | |
105 | - public static int getColorInt(int r, int g, int b) | |
106 | - { | |
107 | - return (r << 16) | (g << 8) | b; | |
108 | - } | |
109 | - | |
110 | -} |
@@ -1,26 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import java.util.function.ObjIntConsumer; | |
4 | -import java.util.function.Supplier; | |
5 | -import java.util.stream.Stream; | |
6 | - | |
7 | -public class HLambda | |
8 | -{ | |
9 | - | |
10 | - public static <T> T get(Supplier<T> supplier) | |
11 | - { | |
12 | - return supplier.get(); | |
13 | - } | |
14 | - | |
15 | - public static <T> void forEach(Stream<T> stream, ObjIntConsumer<T> consumer) | |
16 | - { | |
17 | - Integer[] i = new Integer[] { | |
18 | - 0, | |
19 | - }; | |
20 | - stream.sequential().forEach(object -> { | |
21 | - consumer.accept(object, i[0]); | |
22 | - i[0]++; | |
23 | - }); | |
24 | - } | |
25 | - | |
26 | -} |
@@ -1,96 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import java.io.BufferedReader; | |
4 | -import java.io.File; | |
5 | -import java.io.FileInputStream; | |
6 | -import java.io.IOException; | |
7 | -import java.io.InputStream; | |
8 | -import java.io.InputStreamReader; | |
9 | -import java.util.Hashtable; | |
10 | -import java.util.Map; | |
11 | -import java.util.function.Consumer; | |
12 | -import java.util.regex.Matcher; | |
13 | -import java.util.regex.Pattern; | |
14 | - | |
15 | -public class HLang | |
16 | -{ | |
17 | - | |
18 | - public static Hashtable<String, String> table = new Hashtable<>(); | |
19 | - | |
20 | - public static void register(String key, String value) | |
21 | - { | |
22 | - table.put(key, value); | |
23 | - } | |
24 | - | |
25 | - public static void register(Map<String, String> map) | |
26 | - { | |
27 | - map.entrySet().forEach(entry -> { | |
28 | - register(entry.getKey(), entry.getValue()); | |
29 | - }); | |
30 | - } | |
31 | - | |
32 | - public static Pattern pattern = Pattern.compile("\\s*([^=]+?)\\s*=\\s*(.*?)\\s*"); | |
33 | - | |
34 | - public static void register(String line) | |
35 | - { | |
36 | - Matcher matcher = pattern.matcher(line); | |
37 | - if (matcher.matches()) { | |
38 | - register(matcher.group(1), matcher.group(2)); | |
39 | - } else { | |
40 | - throw new IllegalArgumentException(); | |
41 | - } | |
42 | - } | |
43 | - | |
44 | - public static void register(InputStream in, Consumer<String> onIllegalLine) throws IOException | |
45 | - { | |
46 | - BufferedReader in2 = new BufferedReader(new InputStreamReader(in)); | |
47 | - | |
48 | - String line; | |
49 | - while ((line = in2.readLine()) != null) { | |
50 | - try { | |
51 | - register(line); | |
52 | - } catch (IllegalArgumentException e) { | |
53 | - onIllegalLine.accept(line); | |
54 | - } | |
55 | - } | |
56 | - } | |
57 | - | |
58 | - public static void register(File file) throws IOException | |
59 | - { | |
60 | - try (FileInputStream in = new FileInputStream(file)) { | |
61 | - register(in, line -> { | |
62 | - System.err.println("illegal lang entry: '" + line + "' at " + file.getName()); | |
63 | - }); | |
64 | - } | |
65 | - } | |
66 | - | |
67 | - public static void registerFromFile(String filename) throws IOException | |
68 | - { | |
69 | - try (FileInputStream in = new FileInputStream(filename)) { | |
70 | - register(in, line -> { | |
71 | - System.err.println("illegal lang entry: '" + line + "' at " + filename); | |
72 | - }); | |
73 | - } | |
74 | - } | |
75 | - | |
76 | - public static boolean isLocalizable(String key) | |
77 | - { | |
78 | - return table.containsKey(key); | |
79 | - } | |
80 | - | |
81 | - public static String localize(String key) | |
82 | - { | |
83 | - return table.containsKey(key) ? table.get(key) : key; | |
84 | - } | |
85 | - | |
86 | - public static String localize(String pre, String key, String suf) | |
87 | - { | |
88 | - return localize(pre + "." + key + "." + suf); | |
89 | - } | |
90 | - | |
91 | - public static String localize(String pre, String key) | |
92 | - { | |
93 | - return localize(pre, key, "name"); | |
94 | - } | |
95 | - | |
96 | -} |
@@ -1,84 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import java.io.IOException; | |
4 | -import java.io.OutputStream; | |
5 | -import java.io.PrintStream; | |
6 | -import java.io.UnsupportedEncodingException; | |
7 | -import java.time.ZonedDateTime; | |
8 | -import java.time.format.DateTimeFormatter; | |
9 | -import java.util.ArrayList; | |
10 | -import java.util.function.Consumer; | |
11 | - | |
12 | -public class HLog | |
13 | -{ | |
14 | - | |
15 | - public static ArrayList<String> log = new ArrayList<>(); | |
16 | - public static ArrayList<Consumer<String>> listeners = new ArrayList<>(); | |
17 | - public static PrintStream out; | |
18 | - static | |
19 | - { | |
20 | - try { | |
21 | - out = new PrintStream(new OutputStream() { | |
22 | - | |
23 | - //private ArrayList<Byte> line = new ArrayList<>(); | |
24 | - | |
25 | - private byte[] bytes = new byte[100]; | |
26 | - private int length = 0; | |
27 | - | |
28 | - private boolean beforeR = false; | |
29 | - | |
30 | - @Override | |
31 | - public synchronized void write(int b) throws IOException | |
32 | - { | |
33 | - if (b == '\r') { | |
34 | - beforeR = true; | |
35 | - | |
36 | - log(new String(bytes, 0, length)); | |
37 | - | |
38 | - length = 0; | |
39 | - } else if (b == '\n') { | |
40 | - if (beforeR) { | |
41 | - beforeR = false; | |
42 | - } else { | |
43 | - | |
44 | - log(new String(bytes, 0, length)); | |
45 | - | |
46 | - length = 0; | |
47 | - } | |
48 | - } else { | |
49 | - add((byte) b); | |
50 | - } | |
51 | - } | |
52 | - | |
53 | - private void add(byte b) | |
54 | - { | |
55 | - if (bytes.length == length) { | |
56 | - byte[] tmp = new byte[bytes.length * 3 / 2]; | |
57 | - System.arraycopy(bytes, 0, tmp, 0, length); | |
58 | - bytes = tmp; | |
59 | - } | |
60 | - | |
61 | - bytes[length] = b; | |
62 | - length++; | |
63 | - } | |
64 | - | |
65 | - }, true, "UTF-8"); | |
66 | - } catch (UnsupportedEncodingException e) { | |
67 | - throw new RuntimeException(e); | |
68 | - } | |
69 | - } | |
70 | - | |
71 | - public static void log(String string) | |
72 | - { | |
73 | - String e = "[" + ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss")) + "] " + string; | |
74 | - log.add(e); | |
75 | - listeners.forEach(c -> c.accept(e)); | |
76 | - System.out.println(e); | |
77 | - } | |
78 | - | |
79 | - public static void log(String format, Object... arguments) | |
80 | - { | |
81 | - log(String.format(format, arguments)); | |
82 | - } | |
83 | - | |
84 | -} |
@@ -1,106 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import java.util.Random; | |
4 | - | |
5 | -/** | |
6 | - * 数学的な関数を扱うクラス。 | |
7 | - */ | |
8 | -public class HMath | |
9 | -{ | |
10 | - | |
11 | - public static int randomBetween(Random random, int min, int max) | |
12 | - { | |
13 | - return (int) (random.nextDouble() * (max - min + 1) + min); | |
14 | - } | |
15 | - | |
16 | - public static int randomBetween(int min, int max) | |
17 | - { | |
18 | - return (int) (Math.random() * (max - min + 1) + min); | |
19 | - } | |
20 | - | |
21 | - /** | |
22 | - * log e 10 | |
23 | - */ | |
24 | - public static final double LOG_10 = Math.log(10); | |
25 | - | |
26 | - /** | |
27 | - * log 10 2 | |
28 | - */ | |
29 | - public static final double LOG_10_2 = Math.log(2) / LOG_10; | |
30 | - | |
31 | - /** | |
32 | - * log 10 5 | |
33 | - */ | |
34 | - public static final double LOG_10_5 = Math.log(5) / LOG_10; | |
35 | - | |
36 | - /** | |
37 | - * 与えた数を(1か2か5×10^n)倍の数に切り捨てる。 | |
38 | - * | |
39 | - * @param mspace | |
40 | - * 正の数 | |
41 | - */ | |
42 | - public static double nice(double mspace) | |
43 | - { | |
44 | - double log10 = Math.log10(mspace); | |
45 | - | |
46 | - double integerNumber = Math.floor(log10); | |
47 | - double mod = log10 - integerNumber; | |
48 | - | |
49 | - if (mod > LOG_10_5) { | |
50 | - mod = LOG_10_5; | |
51 | - } else if (mod > LOG_10_2) { | |
52 | - mod = LOG_10_2; | |
53 | - } else { | |
54 | - mod = 0; | |
55 | - } | |
56 | - | |
57 | - return Math.pow(10, integerNumber + mod); | |
58 | - } | |
59 | - | |
60 | - public static double magnitude2(double re, double im) | |
61 | - { | |
62 | - return Math.pow(re, 2) + Math.pow(im, 2); | |
63 | - } | |
64 | - | |
65 | - public static double magnitude(double re, double im) | |
66 | - { | |
67 | - return Math.sqrt(magnitude2(re, im)); | |
68 | - } | |
69 | - | |
70 | - public static boolean isPrime(long a) | |
71 | - { | |
72 | - if (a <= 1) return false; | |
73 | - if (a == 2) return true; | |
74 | - | |
75 | - double limit = Math.sqrt(a) + 1; | |
76 | - | |
77 | - for (long i = 2; i < limit; i++) { | |
78 | - if (a % i == 0) { | |
79 | - return false; | |
80 | - } | |
81 | - } | |
82 | - | |
83 | - return true; | |
84 | - } | |
85 | - | |
86 | - public static double sigmoid(double x, double a) | |
87 | - { | |
88 | - return 1 / (1 + Math.exp(-a * x)); | |
89 | - } | |
90 | - | |
91 | - public static double sigmoid(double x) | |
92 | - { | |
93 | - return 1 / (1 + Math.exp(-x)); | |
94 | - } | |
95 | - | |
96 | - public static double logit(double x, double a) | |
97 | - { | |
98 | - return Math.log(x / (1 - x)) / a; | |
99 | - } | |
100 | - | |
101 | - public static double logit(double x) | |
102 | - { | |
103 | - return Math.log(x / (1 - x)); | |
104 | - } | |
105 | - | |
106 | -} |
@@ -1,133 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import java.util.Comparator; | |
4 | -import java.util.function.ToDoubleFunction; | |
5 | -import java.util.function.ToIntFunction; | |
6 | - | |
7 | -/** | |
8 | - * 数学的な関数を使わないが数値に関することを行うクラス。 | |
9 | - */ | |
10 | -public class HNumber | |
11 | -{ | |
12 | - | |
13 | - public static int trim(int value, int min, int max) | |
14 | - { | |
15 | - if (min > max) return trim(value, max, min); | |
16 | - | |
17 | - if (value < min) return min; | |
18 | - if (value > max) return max; | |
19 | - return value; | |
20 | - } | |
21 | - | |
22 | - public static double trim(double value, double min, double max) | |
23 | - { | |
24 | - if (min > max) return trim(value, max, min); | |
25 | - | |
26 | - if (value < min) return min; | |
27 | - if (value > max) return max; | |
28 | - return value; | |
29 | - } | |
30 | - | |
31 | - /** | |
32 | - * valueがminからmaxの範囲に収まるように、適切に余りを求めます。 | |
33 | - * vale = -1の場合maxを返し、以下流れに沿って適切な値を返します。 | |
34 | - * minがmaxよりも大きな値だった場合、minとmaxを逆に解釈します。 | |
35 | - */ | |
36 | - public static int torus(int value, int min, int max) | |
37 | - { | |
38 | - if (min > max) return torus(value, max, min); | |
39 | - | |
40 | - int n = max - min + 1; | |
41 | - | |
42 | - if (value < 0) { | |
43 | - return (n - 1) - (-value - 1) % n + min; | |
44 | - } else { | |
45 | - return value % n + min; | |
46 | - } | |
47 | - } | |
48 | - | |
49 | - /** | |
50 | - * valueがminかmaxと等しい場合に1、minからmaxの範囲内の場合に2、それ以外の場合に0を返します。 | |
51 | - */ | |
52 | - public static int contains(int value, int min, int max) | |
53 | - { | |
54 | - if (min > max) return contains(value, max, min); | |
55 | - | |
56 | - if (value == min) return 1; | |
57 | - if (value == max) return 1; | |
58 | - | |
59 | - if (value > min && value < max) return 1; | |
60 | - | |
61 | - return 0; | |
62 | - } | |
63 | - | |
64 | - /** | |
65 | - * valueがminかmaxと等しい場合に1、minからmaxの範囲内の場合に2、それ以外の場合に0を返します。 | |
66 | - */ | |
67 | - public static int contains(double value, double min, double max) | |
68 | - { | |
69 | - if (min > max) return contains(value, max, min); | |
70 | - | |
71 | - if (value == min) return 1; | |
72 | - if (value == max) return 1; | |
73 | - | |
74 | - if (value > min && value < max) return 1; | |
75 | - | |
76 | - return 0; | |
77 | - } | |
78 | - | |
79 | - public static int contains( | |
80 | - int valueX, int valueY, | |
81 | - int minX, int maxX, int minY, int maxY) | |
82 | - { | |
83 | - if (minX > maxX) return contains(valueX, valueY, maxX, minX, minY, maxY); | |
84 | - if (minY > maxY) return contains(valueX, valueY, minX, maxX, maxY, minY); | |
85 | - | |
86 | - return Math.min( | |
87 | - contains(valueX, minX, maxX), | |
88 | - contains(valueY, minY, maxY)); | |
89 | - } | |
90 | - | |
91 | - public static int contains( | |
92 | - double valueX, double valueY, | |
93 | - double minX, double maxX, double minY, double maxY) | |
94 | - { | |
95 | - if (minX > maxX) return contains(valueX, valueY, maxX, minX, minY, maxY); | |
96 | - if (minY > maxY) return contains(valueX, valueY, minX, maxX, maxY, minY); | |
97 | - | |
98 | - return Math.min( | |
99 | - contains(valueX, minX, maxX), | |
100 | - contains(valueY, minY, maxY)); | |
101 | - } | |
102 | - | |
103 | - /** | |
104 | - * @see Comparator | |
105 | - */ | |
106 | - public static int compare(double a, double b) | |
107 | - { | |
108 | - if (a < b) return -1; | |
109 | - if (a > b) return 1; | |
110 | - return 0; | |
111 | - } | |
112 | - | |
113 | - public static <T> Comparator<T> createComparator(ToIntFunction<T> function) | |
114 | - { | |
115 | - return (a, b) -> compare(function.applyAsInt(a), function.applyAsInt(b)); | |
116 | - } | |
117 | - | |
118 | - public static <T> Comparator<T> createComparator(ToDoubleFunction<T> function) | |
119 | - { | |
120 | - return (a, b) -> compare(function.applyAsDouble(a), function.applyAsDouble(b)); | |
121 | - } | |
122 | - | |
123 | - public static <T> Comparator<T> createComparatorNegate(ToIntFunction<T> function) | |
124 | - { | |
125 | - return (a, b) -> -compare(function.applyAsInt(a), function.applyAsInt(b)); | |
126 | - } | |
127 | - | |
128 | - public static <T> Comparator<T> createComparatorNegate(ToDoubleFunction<T> function) | |
129 | - { | |
130 | - return (a, b) -> -compare(function.applyAsDouble(a), function.applyAsDouble(b)); | |
131 | - } | |
132 | - | |
133 | -} |
@@ -1,304 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import java.util.ArrayList; | |
4 | -import java.util.Comparator; | |
5 | -import java.util.Hashtable; | |
6 | -import java.util.Map.Entry; | |
7 | -import java.util.Set; | |
8 | -import java.util.regex.Matcher; | |
9 | -import java.util.regex.Pattern; | |
10 | - | |
11 | -public class HString | |
12 | -{ | |
13 | - | |
14 | - static final Pattern PATTERN_NUMBER = Pattern.compile("\\A[0-9]+"); | |
15 | - static final Pattern PATTERN_NOT_NUMBER = Pattern.compile("\\A[^0-9]+"); | |
16 | - | |
17 | - public static int compareWithNumber(String a, String b) | |
18 | - { | |
19 | - return compareWithNumberImpl(a, b, String::compareTo); | |
20 | - } | |
21 | - | |
22 | - public static int compareWithNumberIgnoreCase(String a, String b) | |
23 | - { | |
24 | - return compareWithNumberImpl(a, b, String::compareToIgnoreCase); | |
25 | - } | |
26 | - | |
27 | - public static int compareWithNumberImpl(String a, String b, Comparator<String> comparator) | |
28 | - { | |
29 | - SplitStringStream sssa = new SplitStringStream(a); | |
30 | - SplitStringStream sssb = new SplitStringStream(b); | |
31 | - | |
32 | - while (true) { | |
33 | - boolean inNumber = sssa.isNextStringInNumber(); | |
34 | - | |
35 | - String sa = sssa.getNext(); | |
36 | - String sb = sssb.getNext(); | |
37 | - | |
38 | - if (sa == null) { | |
39 | - if (sb != null) { | |
40 | - return -1; | |
41 | - } else { | |
42 | - return 0; | |
43 | - } | |
44 | - } else { | |
45 | - if (sb == null) { | |
46 | - return 1; | |
47 | - } else { | |
48 | - if (inNumber) { | |
49 | - int la = sa.length(); | |
50 | - int lb = sb.length(); | |
51 | - | |
52 | - int length = Math.max(la, lb); | |
53 | - | |
54 | - sa = rept('0', length - la) + sa; | |
55 | - sb = rept('0', length - lb) + sb; | |
56 | - } | |
57 | - | |
58 | - int compare = comparator.compare(sa, sb); | |
59 | - | |
60 | - if (compare != 0) return compare > 0 ? 1 : -1; | |
61 | - } | |
62 | - } | |
63 | - } | |
64 | - | |
65 | - } | |
66 | - | |
67 | - public static String rept(char ch, int t) | |
68 | - { | |
69 | - StringBuffer sb = new StringBuffer(t); | |
70 | - for (int i = 0; i < t; i++) { | |
71 | - sb.append(ch); | |
72 | - } | |
73 | - return sb.toString(); | |
74 | - } | |
75 | - | |
76 | - public static String rept(String string, int t) | |
77 | - { | |
78 | - StringBuffer sb = new StringBuffer(string.length() + t); | |
79 | - for (int i = 0; i < t; i++) { | |
80 | - sb.append(string); | |
81 | - } | |
82 | - return sb.toString(); | |
83 | - } | |
84 | - | |
85 | - public static class SplitStringStream | |
86 | - { | |
87 | - | |
88 | - private String string; | |
89 | - private boolean inNumber; | |
90 | - | |
91 | - public SplitStringStream(String string) | |
92 | - { | |
93 | - this.string = string; | |
94 | - inNumber = false; | |
95 | - } | |
96 | - | |
97 | - public String getNext() | |
98 | - { | |
99 | - if (string.isEmpty()) return null; | |
100 | - Pattern pattern = inNumber ? PATTERN_NUMBER : PATTERN_NOT_NUMBER; | |
101 | - inNumber = !inNumber; | |
102 | - Matcher m = pattern.matcher(string); | |
103 | - if (!m.find()) return ""; | |
104 | - string = string.substring(m.end()); | |
105 | - return m.group(); | |
106 | - } | |
107 | - | |
108 | - public boolean isNextStringInNumber() | |
109 | - { | |
110 | - return inNumber; | |
111 | - } | |
112 | - | |
113 | - } | |
114 | - | |
115 | - /** | |
116 | - * 小数点以下12桁の文字列表現を作成し、末尾に来た0と.を除去した物を返します。 | |
117 | - */ | |
118 | - public static String toString(double d) | |
119 | - { | |
120 | - StringBuffer sb = new StringBuffer(String.format("%.12f", d)); | |
121 | - while (sb.charAt(sb.length() - 1) == '0') { | |
122 | - sb.setLength(sb.length() - 1); | |
123 | - } | |
124 | - if (sb.charAt(sb.length() - 1) == '.') { | |
125 | - sb.setLength(sb.length() - 1); | |
126 | - } | |
127 | - return sb.toString(); | |
128 | - } | |
129 | - | |
130 | - /** | |
131 | - * 10進数の整数に解析します。 | |
132 | - * | |
133 | - * @see Integer#parseInt(String, int) | |
134 | - * @return null: 解析できなかったとき<br> | |
135 | - * 文字列表現を整数にしたもの | |
136 | - */ | |
137 | - public static Integer parseInt10(String string) | |
138 | - { | |
139 | - try { | |
140 | - return Integer.parseInt(string, 10); | |
141 | - } catch (NumberFormatException e) { | |
142 | - return null; | |
143 | - } | |
144 | - } | |
145 | - | |
146 | - public static char toUpperCase(char ch) | |
147 | - { | |
148 | - if ('a' <= ch && ch <= 'z') ch += 'A' - 'a'; | |
149 | - return ch; | |
150 | - } | |
151 | - | |
152 | - public static char toLowerCase(char ch) | |
153 | - { | |
154 | - if ('A' <= ch && ch <= 'Z') ch += 'a' - 'A'; | |
155 | - return ch; | |
156 | - } | |
157 | - | |
158 | - private static Pattern lineBreak = Pattern.compile("(?:\n|\r\n?)"); | |
159 | - | |
160 | - public static LineProvider getLineProvider(String text) | |
161 | - { | |
162 | - LineProvider lineProvider = new LineProvider(); | |
163 | - Matcher matcher = lineBreak.matcher(text); | |
164 | - | |
165 | - lineProvider.text = text; | |
166 | - lineProvider.lines = 1; | |
167 | - | |
168 | - // | |
169 | - lineProvider.lineNumberToCharacterIndex.put(1, 0); | |
170 | - lineProvider.entries.add(new int[] { | |
171 | - 1, 0, | |
172 | - }); | |
173 | - // | |
174 | - | |
175 | - int indexPreviousStartIndex = 0; | |
176 | - while (matcher.find()) { | |
177 | - | |
178 | - int indexStartMatch = matcher.start(); | |
179 | - int lengthLineBreak = matcher.group(0).length(); | |
180 | - | |
181 | - // | |
182 | - lineProvider.lineNumberToCharacterIndex.put( | |
183 | - lineProvider.lines + 1, | |
184 | - indexStartMatch + lengthLineBreak); | |
185 | - lineProvider.entries.add(new int[] { | |
186 | - lineProvider.lines + 1, | |
187 | - indexStartMatch + lengthLineBreak, | |
188 | - }); | |
189 | - lineProvider.contentLengthes.add(indexStartMatch - indexPreviousStartIndex); | |
190 | - lineProvider.lineLengthes.add(indexStartMatch + lengthLineBreak - indexPreviousStartIndex); | |
191 | - indexPreviousStartIndex = indexStartMatch + lengthLineBreak; | |
192 | - // | |
193 | - | |
194 | - lineProvider.lines++; | |
195 | - } | |
196 | - | |
197 | - // | |
198 | - lineProvider.contentLengthes.add(text.length() - indexPreviousStartIndex); | |
199 | - lineProvider.lineLengthes.add(text.length() - indexPreviousStartIndex); | |
200 | - // | |
201 | - | |
202 | - return lineProvider; | |
203 | - } | |
204 | - | |
205 | - /** | |
206 | - * 行番号と行頭の文字番号の対応。行番号は1から始まり、文字番号は0から始まる。 | |
207 | - */ | |
208 | - public static class LineProvider | |
209 | - { | |
210 | - | |
211 | - protected String text; | |
212 | - protected Hashtable<Integer, Integer> lineNumberToCharacterIndex = new Hashtable<>(); | |
213 | - protected ArrayList<int[]> entries = new ArrayList<>(); | |
214 | - protected ArrayList<Integer> contentLengthes = new ArrayList<>(); | |
215 | - protected ArrayList<Integer> lineLengthes = new ArrayList<>(); | |
216 | - protected int lines; | |
217 | - | |
218 | - public String getText() | |
219 | - { | |
220 | - return text; | |
221 | - } | |
222 | - | |
223 | - public int getLineCount() | |
224 | - { | |
225 | - return lines; | |
226 | - } | |
227 | - | |
228 | - /** | |
229 | - * 文字列長以上の値を入れた場合、最後の行扱いになる。 | |
230 | - */ | |
231 | - public int getLineNumber(int characterIndex) | |
232 | - { | |
233 | - if (characterIndex < 0) { | |
234 | - throw new IllegalArgumentException( | |
235 | - "characterIndex must be >= 0: " + characterIndex); | |
236 | - } | |
237 | - if (characterIndex >= text.length()) { | |
238 | - return entries.get(entries.size() - 1)[0]; | |
239 | - } | |
240 | - | |
241 | - int now = 0; | |
242 | - for (int[] entry : entries) { | |
243 | - if (characterIndex >= entry[1]) { | |
244 | - now = entry[0]; | |
245 | - } else { | |
246 | - return now; | |
247 | - } | |
248 | - } | |
249 | - | |
250 | - return now; | |
251 | - } | |
252 | - | |
253 | - public Set<Entry<Integer, Integer>> entrySet() | |
254 | - { | |
255 | - return lineNumberToCharacterIndex.entrySet(); | |
256 | - } | |
257 | - | |
258 | - public int getStartIndex(int lineNumber) | |
259 | - { | |
260 | - return lineNumberToCharacterIndex.get(lineNumber); | |
261 | - } | |
262 | - | |
263 | - public int getContentLength(int lineNumber) | |
264 | - { | |
265 | - return contentLengthes.get(lineNumber - 1); | |
266 | - } | |
267 | - | |
268 | - public String getContent(int lineNumber) | |
269 | - { | |
270 | - return text.substring( | |
271 | - getStartIndex(lineNumber), | |
272 | - getStartIndex(lineNumber) + getContentLength(lineNumber)); | |
273 | - } | |
274 | - | |
275 | - public int getLineLength(int lineNumber) | |
276 | - { | |
277 | - return lineLengthes.get(lineNumber - 1); | |
278 | - } | |
279 | - | |
280 | - public String getLine(int lineNumber) | |
281 | - { | |
282 | - return text.substring( | |
283 | - getStartIndex(lineNumber), | |
284 | - getStartIndex(lineNumber) + getLineLength(lineNumber)); | |
285 | - } | |
286 | - | |
287 | - } | |
288 | - | |
289 | - /** | |
290 | - * 小数点以下n桁まで表示する文字列を返す。 | |
291 | - * | |
292 | - * @param effectiveDigit | |
293 | - * n | |
294 | - */ | |
295 | - public static String getEffectiveExpression(double value, int effectiveDigit) | |
296 | - { | |
297 | - if (effectiveDigit < 0) { | |
298 | - return String.format("%.0f", value); | |
299 | - } else { | |
300 | - return String.format("%." + effectiveDigit + "f", value); | |
301 | - } | |
302 | - } | |
303 | - | |
304 | -} |
@@ -0,0 +1,54 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import java.awt.Color; | |
4 | +import java.awt.Graphics2D; | |
5 | +import java.awt.image.BufferedImage; | |
6 | +import java.awt.image.ColorModel; | |
7 | +import java.awt.image.DataBuffer; | |
8 | +import java.awt.image.DataBufferInt; | |
9 | +import java.awt.image.DirectColorModel; | |
10 | +import java.awt.image.Raster; | |
11 | +import java.awt.image.WritableRaster; | |
12 | + | |
13 | +public class HAwt | |
14 | +{ | |
15 | + | |
16 | + /** | |
17 | + * 1ピクセルの枠が付いた文字列を色を指定して描画する。 | |
18 | + */ | |
19 | + public static void drawBoldString(Graphics2D g, String str, int x, int y, Color borderColor, Color fillColor) | |
20 | + { | |
21 | + g.setColor(borderColor); | |
22 | + g.drawString(str, x + 1, y); | |
23 | + g.drawString(str, x - 1, y); | |
24 | + g.drawString(str, x, y + 1); | |
25 | + g.drawString(str, x, y - 1); | |
26 | + g.setColor(fillColor); | |
27 | + g.drawString(str, x, y); | |
28 | + } | |
29 | + | |
30 | + /** | |
31 | + * 0x00RRGGBB・透過なし | |
32 | + */ | |
33 | + public static BufferedImage createBufferedImageFromIntArray(int[] data, int width, int height) | |
34 | + { | |
35 | + DataBuffer dataBuffer = new DataBufferInt(data, width * height); | |
36 | + | |
37 | + WritableRaster raster = Raster.createPackedRaster( | |
38 | + dataBuffer, width, height, width, | |
39 | + new int[] { | |
40 | + 0x00ff0000, 0x0000ff00, 0x000000ff, | |
41 | + }, null); | |
42 | + | |
43 | + //Raster.createPackedRaster(dataBuffer, width, height, 32, new Point()); | |
44 | + //colorModel = new ComponentColorModel( | |
45 | + // ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), false, false, | |
46 | + // Transparency.OPAQUE, DataBuffer.TYPE_INT); | |
47 | + | |
48 | + ColorModel colorModel = new DirectColorModel(24, | |
49 | + 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); | |
50 | + | |
51 | + return new BufferedImage(colorModel, raster, false, null); | |
52 | + } | |
53 | + | |
54 | +} |
@@ -0,0 +1,110 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import java.awt.Color; | |
4 | + | |
5 | +public class HColor | |
6 | +{ | |
7 | + | |
8 | + /** | |
9 | + * 定義域を外れたdouble型を適切に丸めてColorインスタンスを作成します。 | |
10 | + * rgbaそれぞれ、int値と同じく0以上256未満の値に丸められます。 | |
11 | + */ | |
12 | + public static Color createColor(double r, double g, double b) | |
13 | + { | |
14 | + return createColor(r, g, b, 255); | |
15 | + } | |
16 | + | |
17 | + /** | |
18 | + * 定義域を外れたint型を適切に丸めてColorインスタンスを作成します。 | |
19 | + */ | |
20 | + public static Color createColor(int r, int g, int b) | |
21 | + { | |
22 | + return createColor(r, g, b, 255); | |
23 | + } | |
24 | + | |
25 | + /** | |
26 | + * 定義域を外れたdouble型を適切に丸めてColorインスタンスを作成します。 | |
27 | + * rgbaそれぞれ、int値と同じく0以上256未満の値に丸められます。 | |
28 | + */ | |
29 | + public static Color createColor(double r, double g, double b, double a) | |
30 | + { | |
31 | + return createColor((int) r, (int) g, (int) b, (int) a); | |
32 | + } | |
33 | + | |
34 | + /** | |
35 | + * 定義域を外れたint型を適切に丸めてColorインスタンスを作成します。 | |
36 | + */ | |
37 | + public static Color createColor(int r, int g, int b, int a) | |
38 | + { | |
39 | + if (r < 0) r = 1; | |
40 | + if (g < 0) g = 1; | |
41 | + if (b < 0) b = 1; | |
42 | + if (a < 0) a = 1; | |
43 | + if (r >= 256) r = 255; | |
44 | + if (g >= 256) g = 255; | |
45 | + if (b >= 256) b = 255; | |
46 | + if (a >= 256) a = 255; | |
47 | + return new Color(r, g, b, a); | |
48 | + } | |
49 | + | |
50 | + /** | |
51 | + * ランダムなrgb値と255のa値を持つ色を生成して返します。 | |
52 | + */ | |
53 | + public static Color createRandomColor() | |
54 | + { | |
55 | + return createRandomRangedColor(0, 255); | |
56 | + } | |
57 | + | |
58 | + /** | |
59 | + * 0から127の範囲でランダムなrgb値と255のa値を持つ色を生成して返します。 | |
60 | + */ | |
61 | + public static Color createRandomDarkColor() | |
62 | + { | |
63 | + return createRandomRangedColor(0, 127); | |
64 | + } | |
65 | + | |
66 | + /** | |
67 | + * 128から255の範囲でランダムなrgb値と255のa値を持つ色を生成して返します。 | |
68 | + */ | |
69 | + public static Color createRandomLightColor() | |
70 | + { | |
71 | + return createRandomRangedColor(128, 255); | |
72 | + } | |
73 | + | |
74 | + /** | |
75 | + * minからmaxの範囲でランダムなrgb値と255のa値を持つ色を生成して返します。 | |
76 | + */ | |
77 | + public static Color createRandomRangedColor(int min, int max) | |
78 | + { | |
79 | + return createColor( | |
80 | + HMath.randomBetween(min, max), | |
81 | + HMath.randomBetween(min, max), | |
82 | + HMath.randomBetween(min, max)); | |
83 | + } | |
84 | + | |
85 | + /** | |
86 | + * 2色a, bの中間色を計算します。 | |
87 | + * Aerが0以下の場合はaを返し、Aerが1以上の場合はbを返します。 | |
88 | + * Aerが小数の場合は2色a, bを適切な比率で混色した色を計算して返します。 | |
89 | + * | |
90 | + * @param Aer | |
91 | + * 0以上1未満の小数 | |
92 | + */ | |
93 | + public static Color createLinearRatioColor(double Aer, Color a, Color b) | |
94 | + { | |
95 | + if (Aer <= 0) return b; | |
96 | + if (Aer >= 1) return a; | |
97 | + | |
98 | + return createColor( | |
99 | + Aer * a.getRed() + (1 - Aer) * b.getRed(), | |
100 | + Aer * a.getGreen() + (1 - Aer) * b.getGreen(), | |
101 | + Aer * a.getBlue() + (1 - Aer) * b.getBlue(), | |
102 | + Aer * a.getAlpha() + (1 - Aer) * b.getAlpha()); | |
103 | + } | |
104 | + | |
105 | + public static int getColorInt(int r, int g, int b) | |
106 | + { | |
107 | + return (r << 16) | (g << 8) | b; | |
108 | + } | |
109 | + | |
110 | +} |
@@ -0,0 +1,26 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import java.util.function.ObjIntConsumer; | |
4 | +import java.util.function.Supplier; | |
5 | +import java.util.stream.Stream; | |
6 | + | |
7 | +public class HLambda | |
8 | +{ | |
9 | + | |
10 | + public static <T> T get(Supplier<T> supplier) | |
11 | + { | |
12 | + return supplier.get(); | |
13 | + } | |
14 | + | |
15 | + public static <T> void forEach(Stream<T> stream, ObjIntConsumer<T> consumer) | |
16 | + { | |
17 | + Integer[] i = new Integer[] { | |
18 | + 0, | |
19 | + }; | |
20 | + stream.sequential().forEach(object -> { | |
21 | + consumer.accept(object, i[0]); | |
22 | + i[0]++; | |
23 | + }); | |
24 | + } | |
25 | + | |
26 | +} |
@@ -0,0 +1,96 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import java.io.BufferedReader; | |
4 | +import java.io.File; | |
5 | +import java.io.FileInputStream; | |
6 | +import java.io.IOException; | |
7 | +import java.io.InputStream; | |
8 | +import java.io.InputStreamReader; | |
9 | +import java.util.Hashtable; | |
10 | +import java.util.Map; | |
11 | +import java.util.function.Consumer; | |
12 | +import java.util.regex.Matcher; | |
13 | +import java.util.regex.Pattern; | |
14 | + | |
15 | +public class HLang | |
16 | +{ | |
17 | + | |
18 | + public static Hashtable<String, String> table = new Hashtable<>(); | |
19 | + | |
20 | + public static void register(String key, String value) | |
21 | + { | |
22 | + table.put(key, value); | |
23 | + } | |
24 | + | |
25 | + public static void register(Map<String, String> map) | |
26 | + { | |
27 | + map.entrySet().forEach(entry -> { | |
28 | + register(entry.getKey(), entry.getValue()); | |
29 | + }); | |
30 | + } | |
31 | + | |
32 | + public static Pattern pattern = Pattern.compile("\\s*([^=]+?)\\s*=\\s*(.*?)\\s*"); | |
33 | + | |
34 | + public static void register(String line) | |
35 | + { | |
36 | + Matcher matcher = pattern.matcher(line); | |
37 | + if (matcher.matches()) { | |
38 | + register(matcher.group(1), matcher.group(2)); | |
39 | + } else { | |
40 | + throw new IllegalArgumentException(); | |
41 | + } | |
42 | + } | |
43 | + | |
44 | + public static void register(InputStream in, Consumer<String> onIllegalLine) throws IOException | |
45 | + { | |
46 | + BufferedReader in2 = new BufferedReader(new InputStreamReader(in)); | |
47 | + | |
48 | + String line; | |
49 | + while ((line = in2.readLine()) != null) { | |
50 | + try { | |
51 | + register(line); | |
52 | + } catch (IllegalArgumentException e) { | |
53 | + onIllegalLine.accept(line); | |
54 | + } | |
55 | + } | |
56 | + } | |
57 | + | |
58 | + public static void register(File file) throws IOException | |
59 | + { | |
60 | + try (FileInputStream in = new FileInputStream(file)) { | |
61 | + register(in, line -> { | |
62 | + System.err.println("illegal lang entry: '" + line + "' at " + file.getName()); | |
63 | + }); | |
64 | + } | |
65 | + } | |
66 | + | |
67 | + public static void registerFromFile(String filename) throws IOException | |
68 | + { | |
69 | + try (FileInputStream in = new FileInputStream(filename)) { | |
70 | + register(in, line -> { | |
71 | + System.err.println("illegal lang entry: '" + line + "' at " + filename); | |
72 | + }); | |
73 | + } | |
74 | + } | |
75 | + | |
76 | + public static boolean isLocalizable(String key) | |
77 | + { | |
78 | + return table.containsKey(key); | |
79 | + } | |
80 | + | |
81 | + public static String localize(String key) | |
82 | + { | |
83 | + return table.containsKey(key) ? table.get(key) : key; | |
84 | + } | |
85 | + | |
86 | + public static String localize(String pre, String key, String suf) | |
87 | + { | |
88 | + return localize(pre + "." + key + "." + suf); | |
89 | + } | |
90 | + | |
91 | + public static String localize(String pre, String key) | |
92 | + { | |
93 | + return localize(pre, key, "name"); | |
94 | + } | |
95 | + | |
96 | +} |
@@ -0,0 +1,84 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.io.OutputStream; | |
5 | +import java.io.PrintStream; | |
6 | +import java.io.UnsupportedEncodingException; | |
7 | +import java.time.ZonedDateTime; | |
8 | +import java.time.format.DateTimeFormatter; | |
9 | +import java.util.ArrayList; | |
10 | +import java.util.function.Consumer; | |
11 | + | |
12 | +public class HLog | |
13 | +{ | |
14 | + | |
15 | + public static ArrayList<String> log = new ArrayList<>(); | |
16 | + public static ArrayList<Consumer<String>> listeners = new ArrayList<>(); | |
17 | + public static PrintStream out; | |
18 | + static | |
19 | + { | |
20 | + try { | |
21 | + out = new PrintStream(new OutputStream() { | |
22 | + | |
23 | + //private ArrayList<Byte> line = new ArrayList<>(); | |
24 | + | |
25 | + private byte[] bytes = new byte[100]; | |
26 | + private int length = 0; | |
27 | + | |
28 | + private boolean beforeR = false; | |
29 | + | |
30 | + @Override | |
31 | + public synchronized void write(int b) throws IOException | |
32 | + { | |
33 | + if (b == '\r') { | |
34 | + beforeR = true; | |
35 | + | |
36 | + log(new String(bytes, 0, length)); | |
37 | + | |
38 | + length = 0; | |
39 | + } else if (b == '\n') { | |
40 | + if (beforeR) { | |
41 | + beforeR = false; | |
42 | + } else { | |
43 | + | |
44 | + log(new String(bytes, 0, length)); | |
45 | + | |
46 | + length = 0; | |
47 | + } | |
48 | + } else { | |
49 | + add((byte) b); | |
50 | + } | |
51 | + } | |
52 | + | |
53 | + private void add(byte b) | |
54 | + { | |
55 | + if (bytes.length == length) { | |
56 | + byte[] tmp = new byte[bytes.length * 3 / 2]; | |
57 | + System.arraycopy(bytes, 0, tmp, 0, length); | |
58 | + bytes = tmp; | |
59 | + } | |
60 | + | |
61 | + bytes[length] = b; | |
62 | + length++; | |
63 | + } | |
64 | + | |
65 | + }, true, "UTF-8"); | |
66 | + } catch (UnsupportedEncodingException e) { | |
67 | + throw new RuntimeException(e); | |
68 | + } | |
69 | + } | |
70 | + | |
71 | + public static void log(String string) | |
72 | + { | |
73 | + String e = "[" + ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss")) + "] " + string; | |
74 | + log.add(e); | |
75 | + listeners.forEach(c -> c.accept(e)); | |
76 | + System.out.println(e); | |
77 | + } | |
78 | + | |
79 | + public static void log(String format, Object... arguments) | |
80 | + { | |
81 | + log(String.format(format, arguments)); | |
82 | + } | |
83 | + | |
84 | +} |
@@ -0,0 +1,106 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import java.util.Random; | |
4 | + | |
5 | +/** | |
6 | + * 数学的な関数を扱うクラス。 | |
7 | + */ | |
8 | +public class HMath | |
9 | +{ | |
10 | + | |
11 | + public static int randomBetween(Random random, int min, int max) | |
12 | + { | |
13 | + return (int) (random.nextDouble() * (max - min + 1) + min); | |
14 | + } | |
15 | + | |
16 | + public static int randomBetween(int min, int max) | |
17 | + { | |
18 | + return (int) (Math.random() * (max - min + 1) + min); | |
19 | + } | |
20 | + | |
21 | + /** | |
22 | + * log e 10 | |
23 | + */ | |
24 | + public static final double LOG_10 = Math.log(10); | |
25 | + | |
26 | + /** | |
27 | + * log 10 2 | |
28 | + */ | |
29 | + public static final double LOG_10_2 = Math.log(2) / LOG_10; | |
30 | + | |
31 | + /** | |
32 | + * log 10 5 | |
33 | + */ | |
34 | + public static final double LOG_10_5 = Math.log(5) / LOG_10; | |
35 | + | |
36 | + /** | |
37 | + * 与えた数を(1か2か5×10^n)倍の数に切り捨てる。 | |
38 | + * | |
39 | + * @param mspace | |
40 | + * 正の数 | |
41 | + */ | |
42 | + public static double nice(double mspace) | |
43 | + { | |
44 | + double log10 = Math.log10(mspace); | |
45 | + | |
46 | + double integerNumber = Math.floor(log10); | |
47 | + double mod = log10 - integerNumber; | |
48 | + | |
49 | + if (mod > LOG_10_5) { | |
50 | + mod = LOG_10_5; | |
51 | + } else if (mod > LOG_10_2) { | |
52 | + mod = LOG_10_2; | |
53 | + } else { | |
54 | + mod = 0; | |
55 | + } | |
56 | + | |
57 | + return Math.pow(10, integerNumber + mod); | |
58 | + } | |
59 | + | |
60 | + public static double magnitude2(double re, double im) | |
61 | + { | |
62 | + return Math.pow(re, 2) + Math.pow(im, 2); | |
63 | + } | |
64 | + | |
65 | + public static double magnitude(double re, double im) | |
66 | + { | |
67 | + return Math.sqrt(magnitude2(re, im)); | |
68 | + } | |
69 | + | |
70 | + public static boolean isPrime(long a) | |
71 | + { | |
72 | + if (a <= 1) return false; | |
73 | + if (a == 2) return true; | |
74 | + | |
75 | + double limit = Math.sqrt(a) + 1; | |
76 | + | |
77 | + for (long i = 2; i < limit; i++) { | |
78 | + if (a % i == 0) { | |
79 | + return false; | |
80 | + } | |
81 | + } | |
82 | + | |
83 | + return true; | |
84 | + } | |
85 | + | |
86 | + public static double sigmoid(double x, double a) | |
87 | + { | |
88 | + return 1 / (1 + Math.exp(-a * x)); | |
89 | + } | |
90 | + | |
91 | + public static double sigmoid(double x) | |
92 | + { | |
93 | + return 1 / (1 + Math.exp(-x)); | |
94 | + } | |
95 | + | |
96 | + public static double logit(double x, double a) | |
97 | + { | |
98 | + return Math.log(x / (1 - x)) / a; | |
99 | + } | |
100 | + | |
101 | + public static double logit(double x) | |
102 | + { | |
103 | + return Math.log(x / (1 - x)); | |
104 | + } | |
105 | + | |
106 | +} |
@@ -0,0 +1,133 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import java.util.Comparator; | |
4 | +import java.util.function.ToDoubleFunction; | |
5 | +import java.util.function.ToIntFunction; | |
6 | + | |
7 | +/** | |
8 | + * 数学的な関数を使わないが数値に関することを行うクラス。 | |
9 | + */ | |
10 | +public class HNumber | |
11 | +{ | |
12 | + | |
13 | + public static int trim(int value, int min, int max) | |
14 | + { | |
15 | + if (min > max) return trim(value, max, min); | |
16 | + | |
17 | + if (value < min) return min; | |
18 | + if (value > max) return max; | |
19 | + return value; | |
20 | + } | |
21 | + | |
22 | + public static double trim(double value, double min, double max) | |
23 | + { | |
24 | + if (min > max) return trim(value, max, min); | |
25 | + | |
26 | + if (value < min) return min; | |
27 | + if (value > max) return max; | |
28 | + return value; | |
29 | + } | |
30 | + | |
31 | + /** | |
32 | + * valueがminからmaxの範囲に収まるように、適切に余りを求めます。 | |
33 | + * vale = -1の場合maxを返し、以下流れに沿って適切な値を返します。 | |
34 | + * minがmaxよりも大きな値だった場合、minとmaxを逆に解釈します。 | |
35 | + */ | |
36 | + public static int torus(int value, int min, int max) | |
37 | + { | |
38 | + if (min > max) return torus(value, max, min); | |
39 | + | |
40 | + int n = max - min + 1; | |
41 | + | |
42 | + if (value < 0) { | |
43 | + return (n - 1) - (-value - 1) % n + min; | |
44 | + } else { | |
45 | + return value % n + min; | |
46 | + } | |
47 | + } | |
48 | + | |
49 | + /** | |
50 | + * valueがminかmaxと等しい場合に1、minからmaxの範囲内の場合に2、それ以外の場合に0を返します。 | |
51 | + */ | |
52 | + public static int contains(int value, int min, int max) | |
53 | + { | |
54 | + if (min > max) return contains(value, max, min); | |
55 | + | |
56 | + if (value == min) return 1; | |
57 | + if (value == max) return 1; | |
58 | + | |
59 | + if (value > min && value < max) return 1; | |
60 | + | |
61 | + return 0; | |
62 | + } | |
63 | + | |
64 | + /** | |
65 | + * valueがminかmaxと等しい場合に1、minからmaxの範囲内の場合に2、それ以外の場合に0を返します。 | |
66 | + */ | |
67 | + public static int contains(double value, double min, double max) | |
68 | + { | |
69 | + if (min > max) return contains(value, max, min); | |
70 | + | |
71 | + if (value == min) return 1; | |
72 | + if (value == max) return 1; | |
73 | + | |
74 | + if (value > min && value < max) return 1; | |
75 | + | |
76 | + return 0; | |
77 | + } | |
78 | + | |
79 | + public static int contains( | |
80 | + int valueX, int valueY, | |
81 | + int minX, int maxX, int minY, int maxY) | |
82 | + { | |
83 | + if (minX > maxX) return contains(valueX, valueY, maxX, minX, minY, maxY); | |
84 | + if (minY > maxY) return contains(valueX, valueY, minX, maxX, maxY, minY); | |
85 | + | |
86 | + return Math.min( | |
87 | + contains(valueX, minX, maxX), | |
88 | + contains(valueY, minY, maxY)); | |
89 | + } | |
90 | + | |
91 | + public static int contains( | |
92 | + double valueX, double valueY, | |
93 | + double minX, double maxX, double minY, double maxY) | |
94 | + { | |
95 | + if (minX > maxX) return contains(valueX, valueY, maxX, minX, minY, maxY); | |
96 | + if (minY > maxY) return contains(valueX, valueY, minX, maxX, maxY, minY); | |
97 | + | |
98 | + return Math.min( | |
99 | + contains(valueX, minX, maxX), | |
100 | + contains(valueY, minY, maxY)); | |
101 | + } | |
102 | + | |
103 | + /** | |
104 | + * @see Comparator | |
105 | + */ | |
106 | + public static int compare(double a, double b) | |
107 | + { | |
108 | + if (a < b) return -1; | |
109 | + if (a > b) return 1; | |
110 | + return 0; | |
111 | + } | |
112 | + | |
113 | + public static <T> Comparator<T> createComparator(ToIntFunction<T> function) | |
114 | + { | |
115 | + return (a, b) -> compare(function.applyAsInt(a), function.applyAsInt(b)); | |
116 | + } | |
117 | + | |
118 | + public static <T> Comparator<T> createComparator(ToDoubleFunction<T> function) | |
119 | + { | |
120 | + return (a, b) -> compare(function.applyAsDouble(a), function.applyAsDouble(b)); | |
121 | + } | |
122 | + | |
123 | + public static <T> Comparator<T> createComparatorNegate(ToIntFunction<T> function) | |
124 | + { | |
125 | + return (a, b) -> -compare(function.applyAsInt(a), function.applyAsInt(b)); | |
126 | + } | |
127 | + | |
128 | + public static <T> Comparator<T> createComparatorNegate(ToDoubleFunction<T> function) | |
129 | + { | |
130 | + return (a, b) -> -compare(function.applyAsDouble(a), function.applyAsDouble(b)); | |
131 | + } | |
132 | + | |
133 | +} |
@@ -0,0 +1,304 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import java.util.ArrayList; | |
4 | +import java.util.Comparator; | |
5 | +import java.util.Hashtable; | |
6 | +import java.util.Map.Entry; | |
7 | +import java.util.Set; | |
8 | +import java.util.regex.Matcher; | |
9 | +import java.util.regex.Pattern; | |
10 | + | |
11 | +public class HString | |
12 | +{ | |
13 | + | |
14 | + static final Pattern PATTERN_NUMBER = Pattern.compile("\\A[0-9]+"); | |
15 | + static final Pattern PATTERN_NOT_NUMBER = Pattern.compile("\\A[^0-9]+"); | |
16 | + | |
17 | + public static int compareWithNumber(String a, String b) | |
18 | + { | |
19 | + return compareWithNumberImpl(a, b, String::compareTo); | |
20 | + } | |
21 | + | |
22 | + public static int compareWithNumberIgnoreCase(String a, String b) | |
23 | + { | |
24 | + return compareWithNumberImpl(a, b, String::compareToIgnoreCase); | |
25 | + } | |
26 | + | |
27 | + public static int compareWithNumberImpl(String a, String b, Comparator<String> comparator) | |
28 | + { | |
29 | + SplitStringStream sssa = new SplitStringStream(a); | |
30 | + SplitStringStream sssb = new SplitStringStream(b); | |
31 | + | |
32 | + while (true) { | |
33 | + boolean inNumber = sssa.isNextStringInNumber(); | |
34 | + | |
35 | + String sa = sssa.getNext(); | |
36 | + String sb = sssb.getNext(); | |
37 | + | |
38 | + if (sa == null) { | |
39 | + if (sb != null) { | |
40 | + return -1; | |
41 | + } else { | |
42 | + return 0; | |
43 | + } | |
44 | + } else { | |
45 | + if (sb == null) { | |
46 | + return 1; | |
47 | + } else { | |
48 | + if (inNumber) { | |
49 | + int la = sa.length(); | |
50 | + int lb = sb.length(); | |
51 | + | |
52 | + int length = Math.max(la, lb); | |
53 | + | |
54 | + sa = rept('0', length - la) + sa; | |
55 | + sb = rept('0', length - lb) + sb; | |
56 | + } | |
57 | + | |
58 | + int compare = comparator.compare(sa, sb); | |
59 | + | |
60 | + if (compare != 0) return compare > 0 ? 1 : -1; | |
61 | + } | |
62 | + } | |
63 | + } | |
64 | + | |
65 | + } | |
66 | + | |
67 | + public static String rept(char ch, int t) | |
68 | + { | |
69 | + StringBuffer sb = new StringBuffer(t); | |
70 | + for (int i = 0; i < t; i++) { | |
71 | + sb.append(ch); | |
72 | + } | |
73 | + return sb.toString(); | |
74 | + } | |
75 | + | |
76 | + public static String rept(String string, int t) | |
77 | + { | |
78 | + StringBuffer sb = new StringBuffer(string.length() + t); | |
79 | + for (int i = 0; i < t; i++) { | |
80 | + sb.append(string); | |
81 | + } | |
82 | + return sb.toString(); | |
83 | + } | |
84 | + | |
85 | + public static class SplitStringStream | |
86 | + { | |
87 | + | |
88 | + private String string; | |
89 | + private boolean inNumber; | |
90 | + | |
91 | + public SplitStringStream(String string) | |
92 | + { | |
93 | + this.string = string; | |
94 | + inNumber = false; | |
95 | + } | |
96 | + | |
97 | + public String getNext() | |
98 | + { | |
99 | + if (string.isEmpty()) return null; | |
100 | + Pattern pattern = inNumber ? PATTERN_NUMBER : PATTERN_NOT_NUMBER; | |
101 | + inNumber = !inNumber; | |
102 | + Matcher m = pattern.matcher(string); | |
103 | + if (!m.find()) return ""; | |
104 | + string = string.substring(m.end()); | |
105 | + return m.group(); | |
106 | + } | |
107 | + | |
108 | + public boolean isNextStringInNumber() | |
109 | + { | |
110 | + return inNumber; | |
111 | + } | |
112 | + | |
113 | + } | |
114 | + | |
115 | + /** | |
116 | + * 小数点以下12桁の文字列表現を作成し、末尾に来た0と.を除去した物を返します。 | |
117 | + */ | |
118 | + public static String toString(double d) | |
119 | + { | |
120 | + StringBuffer sb = new StringBuffer(String.format("%.12f", d)); | |
121 | + while (sb.charAt(sb.length() - 1) == '0') { | |
122 | + sb.setLength(sb.length() - 1); | |
123 | + } | |
124 | + if (sb.charAt(sb.length() - 1) == '.') { | |
125 | + sb.setLength(sb.length() - 1); | |
126 | + } | |
127 | + return sb.toString(); | |
128 | + } | |
129 | + | |
130 | + /** | |
131 | + * 10進数の整数に解析します。 | |
132 | + * | |
133 | + * @see Integer#parseInt(String, int) | |
134 | + * @return null: 解析できなかったとき<br> | |
135 | + * 文字列表現を整数にしたもの | |
136 | + */ | |
137 | + public static Integer parseInt10(String string) | |
138 | + { | |
139 | + try { | |
140 | + return Integer.parseInt(string, 10); | |
141 | + } catch (NumberFormatException e) { | |
142 | + return null; | |
143 | + } | |
144 | + } | |
145 | + | |
146 | + public static char toUpperCase(char ch) | |
147 | + { | |
148 | + if ('a' <= ch && ch <= 'z') ch += 'A' - 'a'; | |
149 | + return ch; | |
150 | + } | |
151 | + | |
152 | + public static char toLowerCase(char ch) | |
153 | + { | |
154 | + if ('A' <= ch && ch <= 'Z') ch += 'a' - 'A'; | |
155 | + return ch; | |
156 | + } | |
157 | + | |
158 | + private static Pattern lineBreak = Pattern.compile("(?:\n|\r\n?)"); | |
159 | + | |
160 | + public static LineProvider getLineProvider(String text) | |
161 | + { | |
162 | + LineProvider lineProvider = new LineProvider(); | |
163 | + Matcher matcher = lineBreak.matcher(text); | |
164 | + | |
165 | + lineProvider.text = text; | |
166 | + lineProvider.lines = 1; | |
167 | + | |
168 | + // | |
169 | + lineProvider.lineNumberToCharacterIndex.put(1, 0); | |
170 | + lineProvider.entries.add(new int[] { | |
171 | + 1, 0, | |
172 | + }); | |
173 | + // | |
174 | + | |
175 | + int indexPreviousStartIndex = 0; | |
176 | + while (matcher.find()) { | |
177 | + | |
178 | + int indexStartMatch = matcher.start(); | |
179 | + int lengthLineBreak = matcher.group(0).length(); | |
180 | + | |
181 | + // | |
182 | + lineProvider.lineNumberToCharacterIndex.put( | |
183 | + lineProvider.lines + 1, | |
184 | + indexStartMatch + lengthLineBreak); | |
185 | + lineProvider.entries.add(new int[] { | |
186 | + lineProvider.lines + 1, | |
187 | + indexStartMatch + lengthLineBreak, | |
188 | + }); | |
189 | + lineProvider.contentLengthes.add(indexStartMatch - indexPreviousStartIndex); | |
190 | + lineProvider.lineLengthes.add(indexStartMatch + lengthLineBreak - indexPreviousStartIndex); | |
191 | + indexPreviousStartIndex = indexStartMatch + lengthLineBreak; | |
192 | + // | |
193 | + | |
194 | + lineProvider.lines++; | |
195 | + } | |
196 | + | |
197 | + // | |
198 | + lineProvider.contentLengthes.add(text.length() - indexPreviousStartIndex); | |
199 | + lineProvider.lineLengthes.add(text.length() - indexPreviousStartIndex); | |
200 | + // | |
201 | + | |
202 | + return lineProvider; | |
203 | + } | |
204 | + | |
205 | + /** | |
206 | + * 行番号と行頭の文字番号の対応。行番号は1から始まり、文字番号は0から始まる。 | |
207 | + */ | |
208 | + public static class LineProvider | |
209 | + { | |
210 | + | |
211 | + protected String text; | |
212 | + protected Hashtable<Integer, Integer> lineNumberToCharacterIndex = new Hashtable<>(); | |
213 | + protected ArrayList<int[]> entries = new ArrayList<>(); | |
214 | + protected ArrayList<Integer> contentLengthes = new ArrayList<>(); | |
215 | + protected ArrayList<Integer> lineLengthes = new ArrayList<>(); | |
216 | + protected int lines; | |
217 | + | |
218 | + public String getText() | |
219 | + { | |
220 | + return text; | |
221 | + } | |
222 | + | |
223 | + public int getLineCount() | |
224 | + { | |
225 | + return lines; | |
226 | + } | |
227 | + | |
228 | + /** | |
229 | + * 文字列長以上の値を入れた場合、最後の行扱いになる。 | |
230 | + */ | |
231 | + public int getLineNumber(int characterIndex) | |
232 | + { | |
233 | + if (characterIndex < 0) { | |
234 | + throw new IllegalArgumentException( | |
235 | + "characterIndex must be >= 0: " + characterIndex); | |
236 | + } | |
237 | + if (characterIndex >= text.length()) { | |
238 | + return entries.get(entries.size() - 1)[0]; | |
239 | + } | |
240 | + | |
241 | + int now = 0; | |
242 | + for (int[] entry : entries) { | |
243 | + if (characterIndex >= entry[1]) { | |
244 | + now = entry[0]; | |
245 | + } else { | |
246 | + return now; | |
247 | + } | |
248 | + } | |
249 | + | |
250 | + return now; | |
251 | + } | |
252 | + | |
253 | + public Set<Entry<Integer, Integer>> entrySet() | |
254 | + { | |
255 | + return lineNumberToCharacterIndex.entrySet(); | |
256 | + } | |
257 | + | |
258 | + public int getStartIndex(int lineNumber) | |
259 | + { | |
260 | + return lineNumberToCharacterIndex.get(lineNumber); | |
261 | + } | |
262 | + | |
263 | + public int getContentLength(int lineNumber) | |
264 | + { | |
265 | + return contentLengthes.get(lineNumber - 1); | |
266 | + } | |
267 | + | |
268 | + public String getContent(int lineNumber) | |
269 | + { | |
270 | + return text.substring( | |
271 | + getStartIndex(lineNumber), | |
272 | + getStartIndex(lineNumber) + getContentLength(lineNumber)); | |
273 | + } | |
274 | + | |
275 | + public int getLineLength(int lineNumber) | |
276 | + { | |
277 | + return lineLengthes.get(lineNumber - 1); | |
278 | + } | |
279 | + | |
280 | + public String getLine(int lineNumber) | |
281 | + { | |
282 | + return text.substring( | |
283 | + getStartIndex(lineNumber), | |
284 | + getStartIndex(lineNumber) + getLineLength(lineNumber)); | |
285 | + } | |
286 | + | |
287 | + } | |
288 | + | |
289 | + /** | |
290 | + * 小数点以下n桁まで表示する文字列を返す。 | |
291 | + * | |
292 | + * @param effectiveDigit | |
293 | + * n | |
294 | + */ | |
295 | + public static String getEffectiveExpression(double value, int effectiveDigit) | |
296 | + { | |
297 | + if (effectiveDigit < 0) { | |
298 | + return String.format("%.0f", value); | |
299 | + } else { | |
300 | + return String.format("%." + effectiveDigit + "f", value); | |
301 | + } | |
302 | + } | |
303 | + | |
304 | +} |
@@ -1,1 +0,0 @@ | ||
1 | -<some-element/> |
@@ -0,0 +1,6 @@ | ||
1 | +package mirrg; | |
2 | + | |
3 | +public class DummyTest | |
4 | +{ | |
5 | + | |
6 | +} |
@@ -1,23 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import static mirrg.util.hydrogen.HMath.*; | |
4 | -import static org.junit.Assert.*; | |
5 | - | |
6 | -import org.junit.Test; | |
7 | - | |
8 | -public class TestHMath | |
9 | -{ | |
10 | - | |
11 | - @Test | |
12 | - public void test_nice() | |
13 | - { | |
14 | - assertEquals(50, nice(56.5), 0.01); | |
15 | - assertEquals(50, nice(50.5), 0.01); | |
16 | - assertEquals(20, nice(49.9), 0.01); | |
17 | - assertEquals(20, nice(20.1), 0.01); | |
18 | - assertEquals(10, nice(19.9), 0.01); | |
19 | - assertEquals(10, nice(10.1), 0.01); | |
20 | - assertEquals(5, nice(9.99), 0.001); | |
21 | - } | |
22 | - | |
23 | -} |
@@ -1,159 +0,0 @@ | ||
1 | -package mirrg.util.hydrogen; | |
2 | - | |
3 | -import static mirrg.util.hydrogen.HString.*; | |
4 | -import static org.junit.Assert.*; | |
5 | - | |
6 | -import org.junit.Test; | |
7 | - | |
8 | -import mirrg.util.hydrogen.HString.LineProvider; | |
9 | - | |
10 | -public class TestHString | |
11 | -{ | |
12 | - | |
13 | - @Test | |
14 | - public void test_tableLineNumberToCharacterIndex() | |
15 | - { | |
16 | - String text = "" | |
17 | - + "\r\n" | |
18 | - + "11111\r" | |
19 | - + "111\n" | |
20 | - + "11111111\r\n" | |
21 | - + "11111\r\n" | |
22 | - + "\r\n" | |
23 | - + "\r\n" | |
24 | - + "1111\r" | |
25 | - + "\r" | |
26 | - + "\r" | |
27 | - + "1111\n" | |
28 | - + "\n" | |
29 | - + "\n"; | |
30 | - | |
31 | - LineProvider lineProvider = HString.getLineProvider(text); | |
32 | - | |
33 | - String[] string = { | |
34 | - "" | |
35 | - }; | |
36 | - lineProvider.entrySet().stream().sequential().sorted((a, b) -> { | |
37 | - if (a.getKey() < b.getKey()) return -1; | |
38 | - if (a.getKey() > b.getKey()) return 1; | |
39 | - return 0; | |
40 | - }).forEach(entry -> { | |
41 | - string[0] += "[" + entry.getKey() + ":" + +entry.getValue() + "]"; | |
42 | - }); | |
43 | - | |
44 | - assertEquals("[1:0][2:2][3:8][4:12][5:22][6:29][7:31][8:33][9:38][10:39][11:40][12:45][13:46][14:47]", | |
45 | - string[0]); | |
46 | - } | |
47 | - | |
48 | - @Test | |
49 | - public void test_getLineNumber() | |
50 | - { | |
51 | - String text = "" | |
52 | - + "\r\n" // 1 | |
53 | - + "11111\r" // 2 | |
54 | - + "111\n" // 3 | |
55 | - + "11111111\r\n" // 4 | |
56 | - + "11111\r\n" // 5 | |
57 | - + "\r\n" // 6 | |
58 | - + "\r\n" // 7 | |
59 | - + "1111\r" // 8 | |
60 | - + "\r" // 9 | |
61 | - + "\r" // 10 | |
62 | - + "1111\n" // 11 | |
63 | - + "\n" // 12 | |
64 | - + "\n"; // 13 | |
65 | - // 14 | |
66 | - | |
67 | - LineProvider lineProvider = HString.getLineProvider(text); | |
68 | - | |
69 | - assertEquals(14, lineProvider.getLineCount()); | |
70 | - | |
71 | - { | |
72 | - int i = 1; | |
73 | - int c = 0; | |
74 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 0: \r | |
75 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 1: \n | |
76 | - i++; | |
77 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 2: 1 | |
78 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 3: 1 | |
79 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 4: 1 | |
80 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 5: 1 | |
81 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 6: 1 | |
82 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 7: \r | |
83 | - i++; | |
84 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 8: 1 | |
85 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 9: 1 | |
86 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 10: 1 | |
87 | - assertEquals(i, lineProvider.getLineNumber(c++)); // 11: \r | |
88 | - | |
89 | - // 最後の文字以降は最後の行扱い | |
90 | - assertEquals(14, lineProvider.getLineNumber(text.length())); // 47: \Z | |
91 | - assertEquals(14, lineProvider.getLineNumber(text.length() + 10)); // 57: null | |
92 | - } | |
93 | - | |
94 | - { | |
95 | - int l = 1; | |
96 | - assertEquals(0, lineProvider.getContentLength(l++)); // 1 | |
97 | - assertEquals(5, lineProvider.getContentLength(l++)); // 2 | |
98 | - assertEquals(3, lineProvider.getContentLength(l++)); // 3 | |
99 | - assertEquals(8, lineProvider.getContentLength(l++)); // 4 | |
100 | - assertEquals(5, lineProvider.getContentLength(l++)); // 5 | |
101 | - assertEquals(0, lineProvider.getContentLength(l++)); // 6 | |
102 | - assertEquals(0, lineProvider.getContentLength(l++)); // 7 | |
103 | - assertEquals(4, lineProvider.getContentLength(l++)); // 8 | |
104 | - assertEquals(0, lineProvider.getContentLength(l++)); // 9 | |
105 | - assertEquals(0, lineProvider.getContentLength(l++)); // 10 | |
106 | - assertEquals(4, lineProvider.getContentLength(l++)); // 11 | |
107 | - assertEquals(0, lineProvider.getContentLength(l++)); // 12 | |
108 | - assertEquals(0, lineProvider.getContentLength(l++)); // 13 | |
109 | - assertEquals(0, lineProvider.getContentLength(l++)); // 14 | |
110 | - } | |
111 | - | |
112 | - assertEquals("11111", lineProvider.getContent(2)); | |
113 | - assertEquals("", lineProvider.getContent(6)); | |
114 | - assertEquals("", lineProvider.getContent(9)); | |
115 | - assertEquals("", lineProvider.getContent(14)); | |
116 | - | |
117 | - { | |
118 | - int l = 1; | |
119 | - assertEquals(2, lineProvider.getLineLength(l++)); | |
120 | - assertEquals(6, lineProvider.getLineLength(l++)); | |
121 | - assertEquals(4, lineProvider.getLineLength(l++)); | |
122 | - assertEquals(10, lineProvider.getLineLength(l++)); | |
123 | - assertEquals(7, lineProvider.getLineLength(l++)); | |
124 | - assertEquals(2, lineProvider.getLineLength(l++)); | |
125 | - assertEquals(2, lineProvider.getLineLength(l++)); | |
126 | - assertEquals(5, lineProvider.getLineLength(l++)); | |
127 | - assertEquals(1, lineProvider.getLineLength(l++)); | |
128 | - assertEquals(1, lineProvider.getLineLength(l++)); | |
129 | - assertEquals(5, lineProvider.getLineLength(l++)); | |
130 | - assertEquals(1, lineProvider.getLineLength(l++)); | |
131 | - assertEquals(1, lineProvider.getLineLength(l++)); | |
132 | - assertEquals(0, lineProvider.getLineLength(l++)); | |
133 | - } | |
134 | - | |
135 | - assertEquals("11111\r", lineProvider.getLine(2)); | |
136 | - assertEquals("\r\n", lineProvider.getLine(6)); | |
137 | - assertEquals("\r", lineProvider.getLine(9)); | |
138 | - assertEquals("", lineProvider.getLine(14)); | |
139 | - | |
140 | - } | |
141 | - | |
142 | - @Test | |
143 | - public void test_getEffectiveExpression() | |
144 | - { | |
145 | - assertEquals("12", getEffectiveExpression(12.125664897, 0)); | |
146 | - assertEquals("12.1", getEffectiveExpression(12.125664897, 1)); | |
147 | - assertEquals("12.13", getEffectiveExpression(12.125664897, 2)); | |
148 | - assertEquals("12.126", getEffectiveExpression(12.125664897, 3)); | |
149 | - assertEquals("12.1257", getEffectiveExpression(12.125664897, 4)); | |
150 | - assertEquals("12.12566", getEffectiveExpression(12.125664897, 5)); | |
151 | - assertEquals("5", getEffectiveExpression(5, 0)); | |
152 | - assertEquals("5.0", getEffectiveExpression(5, 1)); | |
153 | - assertEquals("5.00", getEffectiveExpression(5, 2)); | |
154 | - assertEquals("5.000", getEffectiveExpression(5, 3)); | |
155 | - assertEquals("5.0000", getEffectiveExpression(5, 4)); | |
156 | - assertEquals("5.00000", getEffectiveExpression(5, 5)); | |
157 | - } | |
158 | - | |
159 | -} |
@@ -0,0 +1,23 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import static mirrg.util.hydrogen.v1_0.HMath.*; | |
4 | +import static org.junit.Assert.*; | |
5 | + | |
6 | +import org.junit.Test; | |
7 | + | |
8 | +public class TestHMath | |
9 | +{ | |
10 | + | |
11 | + @Test | |
12 | + public void test_nice() | |
13 | + { | |
14 | + assertEquals(50, nice(56.5), 0.01); | |
15 | + assertEquals(50, nice(50.5), 0.01); | |
16 | + assertEquals(20, nice(49.9), 0.01); | |
17 | + assertEquals(20, nice(20.1), 0.01); | |
18 | + assertEquals(10, nice(19.9), 0.01); | |
19 | + assertEquals(10, nice(10.1), 0.01); | |
20 | + assertEquals(5, nice(9.99), 0.001); | |
21 | + } | |
22 | + | |
23 | +} |
@@ -0,0 +1,160 @@ | ||
1 | +package mirrg.util.hydrogen.v1_0; | |
2 | + | |
3 | +import static mirrg.util.hydrogen.v1_0.HString.*; | |
4 | +import static org.junit.Assert.*; | |
5 | + | |
6 | +import org.junit.Test; | |
7 | + | |
8 | +import mirrg.util.hydrogen.v1_0.HString; | |
9 | +import mirrg.util.hydrogen.v1_0.HString.LineProvider; | |
10 | + | |
11 | +public class TestHString | |
12 | +{ | |
13 | + | |
14 | + @Test | |
15 | + public void test_tableLineNumberToCharacterIndex() | |
16 | + { | |
17 | + String text = "" | |
18 | + + "\r\n" | |
19 | + + "11111\r" | |
20 | + + "111\n" | |
21 | + + "11111111\r\n" | |
22 | + + "11111\r\n" | |
23 | + + "\r\n" | |
24 | + + "\r\n" | |
25 | + + "1111\r" | |
26 | + + "\r" | |
27 | + + "\r" | |
28 | + + "1111\n" | |
29 | + + "\n" | |
30 | + + "\n"; | |
31 | + | |
32 | + LineProvider lineProvider = HString.getLineProvider(text); | |
33 | + | |
34 | + String[] string = { | |
35 | + "" | |
36 | + }; | |
37 | + lineProvider.entrySet().stream().sequential().sorted((a, b) -> { | |
38 | + if (a.getKey() < b.getKey()) return -1; | |
39 | + if (a.getKey() > b.getKey()) return 1; | |
40 | + return 0; | |
41 | + }).forEach(entry -> { | |
42 | + string[0] += "[" + entry.getKey() + ":" + +entry.getValue() + "]"; | |
43 | + }); | |
44 | + | |
45 | + assertEquals("[1:0][2:2][3:8][4:12][5:22][6:29][7:31][8:33][9:38][10:39][11:40][12:45][13:46][14:47]", | |
46 | + string[0]); | |
47 | + } | |
48 | + | |
49 | + @Test | |
50 | + public void test_getLineNumber() | |
51 | + { | |
52 | + String text = "" | |
53 | + + "\r\n" // 1 | |
54 | + + "11111\r" // 2 | |
55 | + + "111\n" // 3 | |
56 | + + "11111111\r\n" // 4 | |
57 | + + "11111\r\n" // 5 | |
58 | + + "\r\n" // 6 | |
59 | + + "\r\n" // 7 | |
60 | + + "1111\r" // 8 | |
61 | + + "\r" // 9 | |
62 | + + "\r" // 10 | |
63 | + + "1111\n" // 11 | |
64 | + + "\n" // 12 | |
65 | + + "\n"; // 13 | |
66 | + // 14 | |
67 | + | |
68 | + LineProvider lineProvider = HString.getLineProvider(text); | |
69 | + | |
70 | + assertEquals(14, lineProvider.getLineCount()); | |
71 | + | |
72 | + { | |
73 | + int i = 1; | |
74 | + int c = 0; | |
75 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 0: \r | |
76 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 1: \n | |
77 | + i++; | |
78 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 2: 1 | |
79 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 3: 1 | |
80 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 4: 1 | |
81 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 5: 1 | |
82 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 6: 1 | |
83 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 7: \r | |
84 | + i++; | |
85 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 8: 1 | |
86 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 9: 1 | |
87 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 10: 1 | |
88 | + assertEquals(i, lineProvider.getLineNumber(c++)); // 11: \r | |
89 | + | |
90 | + // 最後の文字以降は最後の行扱い | |
91 | + assertEquals(14, lineProvider.getLineNumber(text.length())); // 47: \Z | |
92 | + assertEquals(14, lineProvider.getLineNumber(text.length() + 10)); // 57: null | |
93 | + } | |
94 | + | |
95 | + { | |
96 | + int l = 1; | |
97 | + assertEquals(0, lineProvider.getContentLength(l++)); // 1 | |
98 | + assertEquals(5, lineProvider.getContentLength(l++)); // 2 | |
99 | + assertEquals(3, lineProvider.getContentLength(l++)); // 3 | |
100 | + assertEquals(8, lineProvider.getContentLength(l++)); // 4 | |
101 | + assertEquals(5, lineProvider.getContentLength(l++)); // 5 | |
102 | + assertEquals(0, lineProvider.getContentLength(l++)); // 6 | |
103 | + assertEquals(0, lineProvider.getContentLength(l++)); // 7 | |
104 | + assertEquals(4, lineProvider.getContentLength(l++)); // 8 | |
105 | + assertEquals(0, lineProvider.getContentLength(l++)); // 9 | |
106 | + assertEquals(0, lineProvider.getContentLength(l++)); // 10 | |
107 | + assertEquals(4, lineProvider.getContentLength(l++)); // 11 | |
108 | + assertEquals(0, lineProvider.getContentLength(l++)); // 12 | |
109 | + assertEquals(0, lineProvider.getContentLength(l++)); // 13 | |
110 | + assertEquals(0, lineProvider.getContentLength(l++)); // 14 | |
111 | + } | |
112 | + | |
113 | + assertEquals("11111", lineProvider.getContent(2)); | |
114 | + assertEquals("", lineProvider.getContent(6)); | |
115 | + assertEquals("", lineProvider.getContent(9)); | |
116 | + assertEquals("", lineProvider.getContent(14)); | |
117 | + | |
118 | + { | |
119 | + int l = 1; | |
120 | + assertEquals(2, lineProvider.getLineLength(l++)); | |
121 | + assertEquals(6, lineProvider.getLineLength(l++)); | |
122 | + assertEquals(4, lineProvider.getLineLength(l++)); | |
123 | + assertEquals(10, lineProvider.getLineLength(l++)); | |
124 | + assertEquals(7, lineProvider.getLineLength(l++)); | |
125 | + assertEquals(2, lineProvider.getLineLength(l++)); | |
126 | + assertEquals(2, lineProvider.getLineLength(l++)); | |
127 | + assertEquals(5, lineProvider.getLineLength(l++)); | |
128 | + assertEquals(1, lineProvider.getLineLength(l++)); | |
129 | + assertEquals(1, lineProvider.getLineLength(l++)); | |
130 | + assertEquals(5, lineProvider.getLineLength(l++)); | |
131 | + assertEquals(1, lineProvider.getLineLength(l++)); | |
132 | + assertEquals(1, lineProvider.getLineLength(l++)); | |
133 | + assertEquals(0, lineProvider.getLineLength(l++)); | |
134 | + } | |
135 | + | |
136 | + assertEquals("11111\r", lineProvider.getLine(2)); | |
137 | + assertEquals("\r\n", lineProvider.getLine(6)); | |
138 | + assertEquals("\r", lineProvider.getLine(9)); | |
139 | + assertEquals("", lineProvider.getLine(14)); | |
140 | + | |
141 | + } | |
142 | + | |
143 | + @Test | |
144 | + public void test_getEffectiveExpression() | |
145 | + { | |
146 | + assertEquals("12", getEffectiveExpression(12.125664897, 0)); | |
147 | + assertEquals("12.1", getEffectiveExpression(12.125664897, 1)); | |
148 | + assertEquals("12.13", getEffectiveExpression(12.125664897, 2)); | |
149 | + assertEquals("12.126", getEffectiveExpression(12.125664897, 3)); | |
150 | + assertEquals("12.1257", getEffectiveExpression(12.125664897, 4)); | |
151 | + assertEquals("12.12566", getEffectiveExpression(12.125664897, 5)); | |
152 | + assertEquals("5", getEffectiveExpression(5, 0)); | |
153 | + assertEquals("5.0", getEffectiveExpression(5, 1)); | |
154 | + assertEquals("5.00", getEffectiveExpression(5, 2)); | |
155 | + assertEquals("5.000", getEffectiveExpression(5, 3)); | |
156 | + assertEquals("5.0000", getEffectiveExpression(5, 4)); | |
157 | + assertEquals("5.00000", getEffectiveExpression(5, 5)); | |
158 | + } | |
159 | + | |
160 | +} |
@@ -1,1 +0,0 @@ | ||
1 | -<some-resource/> |