nargs;
15 | }
--------------------------------------------------------------------------------
/src/java/tool/tooling/ToolingException.java:
--------------------------------------------------------------------------------
1 | package tool.tooling;
2 |
3 | public class ToolingException extends Exception {
4 | private static final long serialVersionUID = 1L;
5 |
6 | public ToolingException() {
7 | super();
8 | }
9 |
10 | public ToolingException(String message, Throwable cause) {
11 | super(message, cause);
12 | }
13 |
14 | public ToolingException(String message) {
15 | super(message);
16 | }
17 |
18 | public ToolingException(Throwable cause) {
19 | super(cause);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/java/tool/tooling/TypeMapper.java:
--------------------------------------------------------------------------------
1 | package tool.tooling;
2 |
3 | public interface TypeMapper {
4 | public Object map(String str, Class clazz) throws TypeMappingException;
5 | public String supportedValues(Class clazz);
6 | }
--------------------------------------------------------------------------------
/src/java/tool/tooling/TypeMappingException.java:
--------------------------------------------------------------------------------
1 | package tool.tooling;
2 |
3 | public class TypeMappingException extends ToolingException {
4 | private static final long serialVersionUID = 1L;
5 |
6 | public TypeMappingException(Class> clazz, String value) {
7 | super(String.format("Don't now how to map %s to %s", value, clazz));
8 | }
9 |
10 | public TypeMappingException(Throwable t) {
11 | super("Couldn't map", t);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/java/utils/DateTimeUtils.java:
--------------------------------------------------------------------------------
1 | package utils;
2 |
3 | import org.joda.time.format.DateTimeFormat;
4 | import org.joda.time.format.DateTimeFormatter;
5 |
6 | import java.util.Calendar;
7 |
8 | public class DateTimeUtils {
9 |
10 | public static DateTimeFormatter BIRTHDAY_VN_FORMAT = getDateTimeFormatter("dd/MM/yyyy");
11 | public static DateTimeFormatter BIRTHDAY_EN_FORMAT = getDateTimeFormatter("MM/dd/yyyy");
12 | public static final Calendar CALENDAR = Calendar.getInstance();
13 | public static DateTimeFormatter getDateTimeFormatter(String pattern) {
14 | return DateTimeFormat.forPattern(pattern);
15 | }
16 |
17 | public static final int currentYear() {
18 | return CALENDAR.get(Calendar.YEAR);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/java/utils/FileUtils.java:
--------------------------------------------------------------------------------
1 | package utils;
2 |
3 | import com.google.common.hash.HashCode;
4 | import com.google.common.hash.Hashing;
5 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
6 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
7 | import org.iq80.snappy.SnappyInputStream;
8 | import org.iq80.snappy.SnappyOutputStream;
9 | import org.jetbrains.annotations.NotNull;
10 | import org.jetbrains.annotations.Nullable;
11 |
12 | import java.io.*;
13 | import java.nio.charset.Charset;
14 | import java.nio.charset.StandardCharsets;
15 | import java.nio.file.*;
16 | import java.nio.file.attribute.BasicFileAttributes;
17 | import java.nio.file.attribute.PosixFilePermission;
18 | import java.util.*;
19 | import java.util.zip.*;
20 |
21 | public final class FileUtils {
22 |
23 | private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(FileUtils.class);
24 |
25 | /**
26 | * After a successful call, a directory with the given pathname will exist.
27 | * Uses {@link File#mkdir()} to create the directory, if it doesn't exist
28 | * yet.
29 | */
30 | public static void mkdir(@NotNull File file) throws IOException {
31 | if (file.exists()) {
32 | if (!file.isDirectory()) {
33 | throw new IOException(String.format("failed to mkdirs '%s' : exists && !isDirectory", file));
34 | }
35 | } else if (!file.mkdir()) {
36 | throw new IOException(String.format("failed to mkdir '%s'", file));
37 | }
38 | }
39 |
40 | public static InputStream wrapWithSnappy(InputStream inputStream, boolean useSnappy)
41 | throws IOException {
42 | if (useSnappy) {
43 | return new SnappyInputStream(inputStream, true);
44 | } else {
45 | return inputStream;
46 | }
47 | }
48 |
49 | public static OutputStream wrapWithSnappy(OutputStream outputStream, boolean useSnappy)
50 | throws IOException {
51 | if (useSnappy) {
52 | return new SnappyOutputStream(outputStream);
53 | } else {
54 | return outputStream;
55 | }
56 | }
57 |
58 | /**
59 | * After a successful call, a directory with the given pathname will exist.
60 | * Uses {@link File#mkdirs()} to create the directory, if it doesn't exist
61 | * yet.
62 | */
63 | public static void mkdirs(@NotNull File file) throws IOException {
64 | if (file.exists()) {
65 | if (!file.isDirectory()) {
66 | throw new IOException(String.format("failed to mkdirs '%s' : exists && !isDirectory", file));
67 | }
68 | } else if (!file.mkdirs()) {
69 | throw new IOException(String.format("failed to mkdirs '%s'", file));
70 | }
71 | }
72 |
73 | /**
74 | * Creates new file with a given full file name. If a file with the same
75 | * path & name exists then it will be deleted first
76 | *
77 | * @param file full file name
78 | * @throws IOException if delete or create file operation fails
79 | */
80 | public static void createOrReplaceFile(File file) throws IOException {
81 | if (file.exists()) {
82 | LOG.warn("File exists already: {}", file.getAbsolutePath());
83 | if (!file.delete()) {
84 | throw new IOException("Failed to delte file: " + file.getAbsolutePath());
85 | }
86 | }
87 |
88 | if (!file.createNewFile()) {
89 | throw new IOException("Failed to create new file: " + file.getAbsolutePath());
90 | }
91 | }
92 |
93 | public static void moveToDirectory(@NotNull File from, @NotNull File dirTo) throws IOException {
94 | assertDirectoryExists(dirTo);
95 | File to = new File(dirTo, from.getName());
96 | renameTo(from, to);
97 | }
98 |
99 | public static void renameTo(@NotNull File from, @NotNull File to) throws IOException {
100 | assertExists(from);
101 | assertNotExist(to);
102 | if (!from.renameTo(to)) {
103 | throw new IOException(String.format("failed to rename '%s' to '%s'", from, to));
104 | }
105 | }
106 |
107 | /**
108 | * After a successful call, a file (directory) with the given pathname won't
109 | * exist. The directory must be empty in order to be deleted.
110 | *
111 | *
112 | * @throws IOException if operation failed
113 | */
114 | public static void delete(@NotNull File file) throws IOException {
115 | if (file.exists() && !file.delete()) {
116 | if (file.exists()) {
117 | throw new IOException(String.format("failed to delete '%s'", file));
118 | }
119 | }
120 | }
121 |
122 | /**
123 | * After a successful call, inner non-directory files will be disappear
124 | *
125 | *
126 | * @throws IOException if operation failed
127 | */
128 | public static void deleteInnerFiles(@NotNull File file) throws IOException {
129 | if (file.exists() && file.isDirectory()) {
130 | for (File f : file.listFiles()) {
131 | if (f.isFile()) {
132 | delete(f);
133 | }
134 | }
135 | }
136 | }
137 |
138 | /**
139 | * After a successful call, a directory will be empty
140 | *
141 | *
142 | * @throws IOException if operation failed
143 | */
144 | public static void deleteRecursivelyInner(@NotNull File file) throws IOException {
145 | if (file.exists() && file.isDirectory()) {
146 | for (File f : file.listFiles()) {
147 | deleteRecursively(f);
148 | }
149 | }
150 | }
151 |
152 | /**
153 | * Tries to delete a file (directory) with the given pathname and logs
154 | * failed attempts. The directory must be empty in order to be deleted.
155 | */
156 | public static void deleteQuietly(File file) {
157 | try {
158 | delete(file);
159 | } catch (IOException e) {
160 | LOG.warn(e.getMessage());
161 | }
162 | }
163 |
164 | /**
165 | * After a successful call, a file (directory) with the given pathname won't
166 | * exist. This method also works with non-empty directories.
167 | *
168 | *
169 | * @throws IOException if operation failed
170 | */
171 | public static void deleteRecursively(@NotNull File file) throws IOException {
172 | if (file.exists()) {
173 | if (file.isDirectory()) {
174 | File[] childFiles = file.listFiles();
175 | if (childFiles != null) {
176 | for (File childFile : childFiles) {
177 | deleteRecursively(childFile);
178 | }
179 | }
180 | }
181 | delete(file);
182 | }
183 | }
184 |
185 | /**
186 | * Throws an exception if a file (directory) with the given pathname doesn't
187 | * exist.
188 | *
189 | *
190 | * @throws IOException if file doesn't exist
191 | */
192 | public static void assertExists(@NotNull File file) throws IOException {
193 | if (!file.exists()) {
194 | throw new IOException(String.format("file '%s' doesn't exist", file));
195 | }
196 | }
197 |
198 | /**
199 | * Throws an exception if the given pathname doesn't denote an existing
200 | * normal file.
201 | *
202 | *
203 | * @throws IOException if file doesn't exist
204 | */
205 | public static void assertFileExists(@NotNull File file) throws IOException {
206 | if (!file.exists()) {
207 | throw new IOException(String.format("file '%s' doesn't exist", file));
208 | }
209 | if (!file.isFile()) {
210 | throw new IOException(String.format("file '%s' isn't a normal file", file));
211 | }
212 | }
213 |
214 | public static String read(@NotNull BufferedReader bufferedReader, boolean closeInputStream) throws IOException {
215 | StringBuilder stringBuilder = new StringBuilder();
216 | try {
217 | char[] buffer = new char[1024 * 1024];
218 | int size;
219 | while ((size = bufferedReader.read(buffer)) != -1) {
220 | stringBuilder.append(buffer, 0, size);
221 | }
222 | return stringBuilder.toString();
223 | } catch (EOFException e) {
224 | return stringBuilder.toString();
225 | } finally {
226 | if (closeInputStream) {
227 | bufferedReader.close();
228 | }
229 | }
230 | }
231 |
232 | public static String read(@NotNull BufferedReader bufferedReader) throws IOException {
233 | return read(bufferedReader, true);
234 | }
235 |
236 | public static String read(@NotNull InputStream stream) throws IOException {
237 | return read(new BufferedReader(new InputStreamReader(stream)), true);
238 | }
239 |
240 | public static String read(@NotNull InputStream stream, boolean closeInputStream) throws IOException {
241 | return read(new BufferedReader(new InputStreamReader(stream)), closeInputStream);
242 | }
243 |
244 | @NotNull
245 | public static String read(@NotNull File file) throws IOException {
246 | BufferedReader bufferedReader = openBufferReader(file);
247 | StringBuilder stringBuilder = new StringBuilder();
248 | try {
249 | char[] buffer = new char[1024 * 1024];
250 | int size;
251 | while ((size = bufferedReader.read(buffer)) != -1) {
252 | stringBuilder.append(buffer, 0, size);
253 | }
254 | return stringBuilder.toString();
255 | } catch (EOFException e) {
256 | return stringBuilder.toString();
257 | } finally {
258 | bufferedReader.close();
259 | }
260 | }
261 |
262 | private static final String POPULAR_EXTENSIONS = "zip,pdf,mp3,jpg,rar,exe,wmv,doc,avi,"
263 | + "ppt,mpg,tif,wav,mov,psd,wma,sitx,sit,eps,cdr,ai,xls,mp4,txt,m4a,rmvb,bmp,pps,"
264 | + "aif,pub,dwg,gif,qbb,mpeg,indd,swf,asf,png,dat,rm,mdb,chm,jar,htm,dvf,dss,dmg,iso,"
265 | + "flv,wpd,cda,m4b,7z,gz,fla,qxd,rtf,aiff,msi,jpeg,3gp,cdl,vob,ace,m4p,divx,html,pst,"
266 | + "cab,ttf,xtm,hqx,qbw,sea,ptb,bin,mswmm,ifo,tgz,log,dll,mcd,ss,m4v,eml,mid,ogg,ram,"
267 | + "lnk,torrent,ses,mp2,vcd,bat,asx,ps,bup,cbr,amr,wps,sql,docx,xlsx,pptx,csv,json,bin";
268 | private static final Set EXTENSIONS = new ObjectOpenHashSet<>(
269 | Arrays.asList(POPULAR_EXTENSIONS.split(",")));
270 |
271 | public static File getFileWithSuffix(File dir, File input, String suffix) {
272 | String name = input.getName();
273 | int index = name.lastIndexOf('.');
274 | if (index > 0) {
275 | String extendsion = name.substring(index + 1);
276 | if (EXTENSIONS.contains(extendsion)) {
277 | name = name.substring(0, index) + "." + suffix + "." + extendsion;
278 | } else {
279 | name = name + "." + suffix;
280 | }
281 | } else {
282 | name = name + "." + suffix;
283 | }
284 | return new File(dir, name);
285 | }
286 |
287 | public static File getFileWithSuffix(File input, String suffix) {
288 | return getFileWithSuffix(input.getParentFile(), input, suffix);
289 | }
290 |
291 | /**
292 | * Method reads last line from file if file exists and not empty.
293 | *
294 | *
295 | * @param path file to read
296 | * @return last line in file, null in case of empty file
297 | *
298 | * @throws IOException
299 | */
300 | @Nullable
301 | public static String readLastLine(@NotNull Path path) throws IOException {
302 | String prevLine = null;
303 | try (BufferedReader br = Files.newBufferedReader(path, Charset.forName("UTF-8"))) {
304 | String line;
305 | while ((line = br.readLine()) != null) {
306 | prevLine = line;
307 | }
308 | }
309 |
310 | return prevLine;
311 | }
312 |
313 | @NotNull
314 | public static List readList(@NotNull File file) throws IOException {
315 | List res = new ObjectArrayList<>();
316 | addLinesToCollection(file, res);
317 | return res;
318 | }
319 |
320 | public static void addLinesToCollection(
321 | @NotNull File file, @NotNull Collection collection)
322 | throws IOException {
323 | String line;
324 | try (BufferedReader bufferedReader = openBufferReader(file)) {
325 | while ((line = bufferedReader.readLine()) != null) {
326 | collection.add(line);
327 | }
328 | bufferedReader.close();
329 | } catch (EOFException ignored) {
330 | }
331 | }
332 |
333 | @NotNull
334 | public static BufferedWriter write(@NotNull BufferedWriter bw, @NotNull String... values) throws IOException {
335 | for (String val : values) {
336 | bw.append(val);
337 | }
338 | return bw;
339 | }
340 |
341 | @NotNull
342 | public static BufferedWriter writeln(@NotNull BufferedWriter bw, @NotNull String... values) throws IOException {
343 | write(bw, values).newLine();
344 | return bw;
345 | }
346 |
347 | public static File write(File output, Collection extends Object> list) throws IOException {
348 | try (BufferedWriter writer = openWriter(output)) {
349 | for (Object obj : list) {
350 | write(writer, obj.toString()).newLine();
351 | }
352 | writer.close();
353 | }
354 | return output;
355 | }
356 |
357 | public static File write(File output, Object[] list) throws IOException {
358 | try (BufferedWriter writer = openWriter(output)) {
359 | for (Object obj : list) {
360 | write(writer, obj.toString()).newLine();
361 | }
362 | writer.close();
363 | }
364 | return output;
365 | }
366 |
367 | /**
368 | * Throws an exception if the given pathname doesn't denote an existing
369 | * directory.
370 | *
371 | *
372 | * @throws IOException if file doesn't exist
373 | */
374 | public static void assertDirectoryExists(@NotNull File file) throws IOException {
375 | if (!file.exists()) {
376 | throw new IOException(String.format("file '%s' doesn't exist", file));
377 | }
378 | if (!file.isDirectory()) {
379 | throw new IOException(String.format("file '%s' isn't a directory", file));
380 | }
381 | }
382 |
383 | /**
384 | * Throws an exception if a file (directory) with the given pathname exists.
385 | *
386 | *
387 | * @throws IOException if file exist
388 | */
389 | public static void assertNotExist(@NotNull File file) throws IOException {
390 | if (file.exists()) {
391 | throw new IOException(String.format("file '%s' exists", file));
392 | }
393 | }
394 |
395 | public static void replaceFile(File src, File dest) throws IOException {
396 | delete(dest);
397 | renameTo(src, dest);
398 | }
399 |
400 | /**
401 | * Calculates the checksum file's pathname for a hadoop SequenceFile denoted
402 | * by the given pathname.
403 | */
404 | public static File getSequenceFileCrcFile(@NotNull File file) {
405 | return new File(file.getParentFile(), "." + file.getName() + ".crc");
406 |
407 | }
408 |
409 | public static void copyFileIfExists(File from, File to, int bufferSize) throws IOException {
410 | if (from.exists()) {
411 | copyFile(from, to, bufferSize);
412 | }
413 | }
414 |
415 | public static void linkOrCopyFile(File from, File to, byte[] buffer) throws IOException {
416 | try {
417 | Files.createLink(to.toPath(), from.toPath());
418 | } catch (IOException ex) {
419 | copyFile(from, to, buffer);
420 | }
421 | }
422 |
423 | public static void linkOrCopyFile(File from, File to, int bufferSize) throws IOException {
424 | try {
425 | Files.createLink(to.toPath(), from.toPath());
426 | } catch (IOException ex) {
427 | copyFile(from, to, bufferSize);
428 | }
429 | }
430 |
431 | public static void copyFile(@NotNull File from, @NotNull File to, int bufferSize) throws IOException {
432 | if (bufferSize <= 0) {
433 | throw new IllegalArgumentException("bufferSize <= 0");
434 | }
435 | assertFileExists(from);
436 | assertNotExist(to);
437 | copyFile(from, to, new byte[bufferSize]);
438 | }
439 |
440 | public static void copyFile(@NotNull File from, @NotNull File to) throws IOException {
441 | assertFileExists(from);
442 | copyFile(from, to, new byte[DEFAULT_BUFFER_SIZE]);
443 | }
444 |
445 | public static void copy(InputStream input, OutputStream output) throws IOException {
446 | copy(input, output, new byte[DEFAULT_BUFFER_SIZE >>> 2]);
447 | }
448 |
449 | public static void copy(InputStream input, OutputStream output, byte[] buffer) throws IOException {
450 | // we don't need to fill buffer fully before writing it into output,
451 | // because input.read does this almost always - TODO check
452 | int cnt;
453 | while ((cnt = input.read(buffer)) != -1) {
454 | output.write(buffer, 0, cnt);
455 | }
456 | }
457 |
458 | /**
459 | * Closes all streams independently.
460 | */
461 | public static void closeAll(Iterable extends Closeable> closeables) throws IOException {
462 | if (closeables == null) {
463 | return;
464 | }
465 | IOException e2 = null;
466 | for (Closeable closeable : closeables) {
467 | if (closeable == null) {
468 | continue;
469 | }
470 | try {
471 | closeable.close();
472 | } catch (IOException e) {
473 | e2 = e;
474 | }
475 | }
476 | if (e2 != null) {
477 | throw e2;
478 | }
479 | }
480 |
481 | /**
482 | * Closes all streams independently.
483 | */
484 | public static void closeAll(Closeable... closeables) throws IOException {
485 | if (closeables != null) {
486 | closeAll(Arrays.asList(closeables));
487 | }
488 | }
489 |
490 | private static void copyFile(File from, File to, byte[] buffer) throws IOException {
491 | FileInputStream input = null;
492 | FileOutputStream output = null;
493 | try {
494 | input = new FileInputStream(from);
495 | output = new FileOutputStream(to);
496 | copy(input, output, buffer);
497 | } finally {
498 | closeAll(input, output);
499 | }
500 | }
501 |
502 | public static void linkOrCopyRecursively(File from, File to, int bufferSize) throws IOException {
503 | assertExists(from);
504 | assertNotExist(to);
505 | linkOrCopyRecursively(from, to, new byte[bufferSize]);
506 | }
507 |
508 | private static void linkOrCopyRecursively(File from, File to, byte[] buffer) throws IOException {
509 | if (from.isDirectory()) {
510 | FileUtils.mkdir(to);
511 | File[] childFiles = from.listFiles();
512 | if (childFiles != null) {
513 | for (File childFile : childFiles) {
514 | linkOrCopyRecursively(childFile, new File(to, childFile.getName()), buffer);
515 | }
516 | }
517 | } else {
518 | linkOrCopyFile(from, to, buffer);
519 | }
520 | }
521 |
522 | public static void copyRecursively(@NotNull File from, @NotNull File to, int bufferSize) throws IOException {
523 | if (bufferSize <= 0) {
524 | throw new IllegalArgumentException("bufferSize <= 0");
525 | }
526 | assertExists(from);
527 | assertNotExist(to);
528 | copyRecursively(from, to, new byte[bufferSize]);
529 | }
530 |
531 | /**
532 | * Copies fromDir/* to toDir/ recursively. Overwrites existing files.
533 | *
534 | *
535 | * @throws IOException
536 | */
537 | public static void copyDirectoryContents(File fromDir, File toDir, int bufferSize) throws IOException {
538 | if (fromDir == null) {
539 | throw new IllegalArgumentException("fromDir is null");
540 | }
541 | if (toDir == null) {
542 | throw new IllegalArgumentException("toDir is null");
543 | }
544 | if (!fromDir.equals(toDir)) {
545 | if (bufferSize <= 0) {
546 | throw new IllegalArgumentException("bufferSize <= 0");
547 | }
548 | assertDirectoryExists(fromDir);
549 | mkdirs(toDir);
550 | copyRecursively(fromDir, toDir, new byte[bufferSize]);
551 | }
552 | }
553 |
554 | public static void copyDirectoryContents(File fromDir, File toDir) throws IOException {
555 | copyDirectoryContents(fromDir, toDir, DEFAULT_BUFFER_SIZE);
556 | }
557 |
558 | public static void copyFileToDirectory(File fromFile, File toDir) throws IOException {
559 | copyContent(fromFile, new File(toDir, fromFile.getName()));
560 | }
561 |
562 | private static void copyRecursively(File from, File to, byte[] buffer) throws IOException {
563 | if (from.isDirectory()) {
564 | FileUtils.mkdir(to);
565 | File[] childFiles = from.listFiles();
566 | if (childFiles != null) {
567 | for (File childFile : childFiles) {
568 | copyRecursively(childFile, new File(to, childFile.getName()), buffer);
569 | }
570 | }
571 | } else {
572 | copyFile(from, to, buffer);
573 | }
574 | }
575 |
576 | private FileUtils() {
577 | }
578 |
579 | @NotNull
580 | public static File removeSuffix(@NotNull File file, @NotNull String suffix) throws IOException {
581 | if (suffix.isEmpty()) {
582 | throw new IllegalArgumentException("Suffix to remove is empty. File: " + file.getCanonicalPath());
583 | }
584 | String path = file.getCanonicalPath();
585 | if (!path.endsWith(suffix)) {
586 | throw new IllegalArgumentException("File doesn't ends with " + suffix
587 | + ". File: " + file.getCanonicalPath());
588 | }
589 |
590 | path = path.substring(0, path.length() - suffix.length());
591 | File renamed = new File(path);
592 | FileUtils.renameTo(file, renamed);
593 | return renamed;
594 | }
595 |
596 | @NotNull
597 | public static File addSuffix(@NotNull File file, @NotNull String suffix) throws IOException {
598 | if (suffix.isEmpty()) {
599 | throw new IllegalArgumentException("Suffix to remove is empty. File: " + file.getCanonicalPath());
600 | }
601 | String path = file.getCanonicalPath();
602 | if (path.endsWith(suffix)) {
603 | throw new IllegalArgumentException("File ends with " + suffix
604 | + " already. File: " + file.getCanonicalPath());
605 | }
606 | File renamed = new File(path + suffix);
607 | FileUtils.renameTo(file, renamed);
608 | return renamed;
609 | }
610 |
611 | public static boolean writeTo(@NotNull BufferedWriter br, boolean first, @NotNull String line)
612 | throws IOException {
613 | if (first) {
614 | first = false;
615 | } else {
616 | br.newLine();
617 | }
618 | br.write(line);
619 |
620 | return first;
621 | }
622 |
623 | public static void assertNotInFolder(@NotNull FilenameFilter filterToProhibit, @NotNull File... folders)
624 | throws IOException {
625 | for (File folder : folders) {
626 | String[] names = folder.list(filterToProhibit);
627 | if (names == null) {
628 | throw new IOException(folder + " is not a directory");
629 | } else {
630 | if (names.length > 0) {
631 | throw new IOException(folder + " contains prohibited files");
632 | }
633 | }
634 | }
635 | }
636 |
637 | public static void setPermissionsRecursively(
638 | @NotNull Path path, @NotNull Set permissions) throws IOException {
639 | Files.walkFileTree(path, new RecursivePermissionUpdater(permissions));
640 | }
641 |
642 | private static class RecursivePermissionUpdater extends SimpleFileVisitor {
643 |
644 | private final Set permissions;
645 |
646 | private RecursivePermissionUpdater(@NotNull Set permissions) {
647 | this.permissions = permissions;
648 | }
649 |
650 | @NotNull
651 | @Override
652 | public FileVisitResult visitFile(@NotNull Path file, @NotNull BasicFileAttributes attrs) throws IOException {
653 | Files.setPosixFilePermissions(file, permissions);
654 | return FileVisitResult.CONTINUE;
655 | }
656 |
657 | @NotNull
658 | @Override
659 | public FileVisitResult postVisitDirectory(@NotNull Path dir, @NotNull IOException exc) throws IOException {
660 | Files.setPosixFilePermissions(dir, permissions);
661 | return FileVisitResult.CONTINUE;
662 | }
663 |
664 | }
665 |
666 | public static void copyIfSourceExists(@NotNull Path source, @NotNull Path destination) throws IOException {
667 | if (Files.exists(source)) {
668 | Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
669 | }
670 | }
671 |
672 | @NotNull
673 | public static Path getDestination(@NotNull Path symlink) throws IOException {
674 | if (Files.exists(symlink)) {
675 | if (Files.isSymbolicLink(symlink)) {
676 | return Files.readSymbolicLink(symlink);
677 | } else {
678 | throw new IOException(symlink.toString() + " should be symbolic link.");
679 | }
680 | } else {
681 | throw new IOException(symlink.toString() + " doesn't exists.");
682 | }
683 | }
684 |
685 | public static final String DIRECTORY_NAME_TMP = "tmp";
686 |
687 | /*
688 | * create a not-existed temporary file
689 | * created file will be existed with empty content after this function
690 | */
691 | public static File tmpFile(File sample, File temp, boolean gzip) throws IOException {
692 | String fileName = sample.getName() + ".tmp";
693 | String gz = gzip ? ".gz" : "";
694 | File tmp = new File(temp, fileName + gz);
695 | int id = 0;
696 | while (tmp.exists()) {
697 | tmp = new File(temp, fileName + id + gz);
698 | id++;
699 | }
700 | try {
701 | try (BufferedWriter writer = openWriter(tmp)) {
702 | writer.write("");
703 | }
704 | } catch (IOException e) {
705 | throw new IOException("Can't create tmp file " + tmp.getPath());
706 | }
707 | return tmp;
708 | }
709 |
710 | /*
711 | * create a not-existed temporary file
712 | * default is support gzip format
713 | */
714 | public static File tmpFile(File sample, File temp) throws IOException {
715 | return tmpFile(sample, temp, true);
716 | }
717 |
718 | public static File tmpDir(File sample, File temp) throws IOException {
719 | String fileName = sample.getName() + "_tmp";
720 | File tmp = new File(temp, fileName);
721 | int id = 0;
722 | while (tmp.exists()) {
723 | tmp = new File(temp, fileName + id);
724 | id++;
725 | }
726 | mkdir(tmp);
727 | return tmp;
728 | }
729 |
730 | /*
731 | * create a not-existed temporary file
732 | */
733 | public static File tmpFile(File sample, boolean gzip) throws IOException {
734 | File temp = sample.getParentFile();
735 | if (temp == null) {
736 | throw new IOException("Don't support temporary files for current directory");
737 | }
738 | File tmpDir = new File(temp, DIRECTORY_NAME_TMP);
739 | if (tmpDir.exists() && tmpDir.isDirectory()) {
740 | temp = tmpDir;
741 | }
742 | return tmpFile(sample, temp, gzip);
743 | }
744 |
745 | public static final String DIRECTORY_NAME_BACKUP = "backup";
746 |
747 | public static File createBackupFile(File sample, File directory) throws IOException {
748 | String name = sample.getName();
749 | String extension = "";
750 | int index = name.lastIndexOf('.');
751 | if (index >= 0) {
752 | extension = name.substring(index);
753 | name = name.substring(0, index);
754 | }
755 | File backup = new File(directory, name + extension);
756 | int id = 0;
757 | while (backup.exists()) {
758 | backup = new File(directory, name + ".bak" + ++id + extension);
759 | }
760 | com.google.common.io.Files.copy(sample, backup);
761 | return backup;
762 | }
763 |
764 | public static File createBackupFile(File sample) throws IOException {
765 | File directory = sample.getParentFile();
766 | if (directory != null) {
767 | File backup = new File(directory, DIRECTORY_NAME_BACKUP);
768 | if (backup.exists() && backup.isDirectory()) {
769 | directory = backup;
770 | }
771 | return createBackupFile(sample, directory);
772 | } else {
773 | throw new IOException("Don't support backup for current directory");
774 | }
775 | }
776 |
777 | private static int DEFAULT_BUFFER_SIZE = 4 * 1024 * 1024;
778 |
779 | public static long copyContent(BufferedReader reader, BufferedWriter writer) throws IOException {
780 | long lines = 0;
781 | while (true) {
782 | try {
783 | String line = reader.readLine();
784 | if (line == null) {
785 | break;
786 | }
787 | writer.write(line);
788 | writer.newLine();
789 | lines++;
790 | } catch (EOFException e) {
791 | // ignore this
792 | LOG.warn("Catched EOFException - ", e);
793 | break;
794 | }
795 | }
796 | writer.close();
797 | return lines;
798 | }
799 |
800 | public static long lines(File file) {
801 | long count = 0;
802 | try {
803 | BufferedReader reader = openBufferReader(file);
804 | for (String line = reader.readLine(); line != null; line = reader.readLine()) {
805 | count++;
806 | }
807 | reader.close();
808 | } catch (IOException ignore) {
809 | }
810 | return count;
811 | }
812 |
813 | public static long copyContent(File from, BufferedWriter writer) throws IOException {
814 | long lines;
815 | try (BufferedReader reader = openBufferReader(from);) {
816 | lines = copyContent(reader, writer);
817 | }
818 | return lines;
819 | }
820 |
821 | // return number of line
822 | public static long copyContent(File from, File to) throws IOException {
823 | long lines;
824 | try (BufferedReader reader = openBufferReader(from); BufferedWriter writer = openWriter(to)) {
825 | lines = copyContent(reader, writer);
826 | }
827 | return lines;
828 | }
829 |
830 | public static boolean isContentEqual(File file1, File file2) throws IOException {
831 | HashCode hash1 = com.google.common.io.Files.hash(file1, Hashing.sha1());
832 | HashCode hash2 = com.google.common.io.Files.hash(file2, Hashing.sha1());
833 | return hash1.equals(hash2);
834 | }
835 |
836 | public static BufferedReader openBufferReader(File file) throws IOException {
837 | return openBufferReader(file, false);
838 | }
839 |
840 | public static BufferedReader openBufferReader(InputStream inputStream) throws IOException {
841 | return new BufferedReader(new InputStreamReader(inputStream), DEFAULT_BUFFER_SIZE);
842 | }
843 |
844 | public static BufferedReader openBufferReader(File file, final boolean forceGzip) throws IOException {
845 | if (forceGzip || file.getName().endsWith(".gz")) {
846 | return new BufferedReader(new InputStreamReader(new GZIPInputStream(
847 | new BufferedInputStream(new FileInputStream(file)))),
848 | (int) Math.min(DEFAULT_BUFFER_SIZE, file.length() + 1));
849 | } else {
850 | return new BufferedReader(new InputStreamReader(openInputStream(file)),
851 | (int) Math.min(DEFAULT_BUFFER_SIZE, file.length() + 1));
852 | }
853 | }
854 |
855 | public static InputStream openInputStream(File file) throws IOException {
856 | if (file.getName().endsWith(".gz")) {
857 | return new GZIPInputStream(new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE >> 1));
858 | } else {
859 | return new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
860 | }
861 | }
862 |
863 | public static InputStream openZipInputStreamFromSingleZippedFile(File file) throws IOException {
864 | ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
865 | zipInputStream.getNextEntry();
866 | return zipInputStream;
867 | }
868 |
869 | public static OutputStream openOutputStream(File file) throws IOException {
870 | if (file.getName().endsWith(".gz")) {
871 | return new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file), DEFAULT_BUFFER_SIZE >>> 1)) {
872 | {
873 | def.setLevel(Deflater.BEST_SPEED);
874 | }
875 |
876 | };
877 | } else {
878 | return new BufferedOutputStream(new FileOutputStream(file), DEFAULT_BUFFER_SIZE);
879 | }
880 | }
881 |
882 | /**
883 | * Open a print stream to write into a file
884 | *
885 | *
886 | * @param file
887 | * @return
888 | */
889 | public static BufferedWriter openWriter(File file) throws IOException {
890 | return openWriter(openOutputStream(file));
891 | }
892 |
893 | public static BufferedWriter openWriter(OutputStream outputStream) throws IOException {
894 | return new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), DEFAULT_BUFFER_SIZE);
895 | }
896 |
897 | /**
898 | * Open print stream to append into a file
899 | */
900 | public static BufferedWriter openAppendWriter(File file) throws Exception {
901 | if (!file.getName().endsWith(".gz")) {
902 | return new BufferedWriter(new FileWriter(file, true));
903 | } else {
904 | FileOutputStream fout = new FileOutputStream(file, true);
905 | GZIPOutputStream zstream = new GZIPOutputStream(fout, DEFAULT_BUFFER_SIZE >>> 1) {
906 | {
907 | def.setLevel(Deflater.BEST_SPEED);
908 | }
909 |
910 | };
911 | return new BufferedWriter(new OutputStreamWriter(zstream), DEFAULT_BUFFER_SIZE);
912 | }
913 | }
914 |
915 |
916 | public static void appendContentToFile(File inputfile, File outputFile) throws Exception {
917 | BufferedWriter newWriter = openAppendWriter(outputFile);
918 | copyContent(inputfile, newWriter);
919 |
920 | }
921 |
922 | /**
923 | * including sub-directory's children, too
924 | */
925 | public static Iterator getChildrenFileIteration(final File file) {
926 | final Queue queue = new LinkedList<>();
927 | if (file != null && file.exists() && file.isDirectory()) {
928 | queue.add(file);
929 | }
930 | return new Iterator() {
931 | @Override
932 | public boolean hasNext() {
933 | return queue.size() > 0;
934 | }
935 |
936 | @Override
937 | public File next() {
938 | File file = queue.poll();
939 | if (file != null && file.isDirectory()) {
940 | queue.addAll(Arrays.asList(file.listFiles()));
941 | }
942 | return file;
943 | }
944 | };
945 | }
946 |
947 | public static void zipFiles(List inputs, File zipFile) throws IOException {
948 | zipFiles(inputs, zipFile, false);
949 | }
950 |
951 | public static void zipFiles(List inputs, File zipFile, boolean deleteAfterZip) throws IOException {
952 | FileOutputStream fout = null;
953 | ZipOutputStream zout = null;
954 | for (File file : inputs) {
955 | if (file == null || !file.exists()) {
956 | continue;
957 | }
958 | if (fout == null) {
959 | fout = new FileOutputStream(zipFile);
960 | zout = new ZipOutputStream(new BufferedOutputStream(fout));
961 | }
962 | byte[] buffer = new byte[100 * 1024];
963 | InputStream fin = openInputStream(file);
964 | zout.putNextEntry(new ZipEntry(file.getName()));
965 | int length;
966 | while ((length = fin.read(buffer)) > 0) {
967 | zout.write(buffer, 0, length);
968 | }
969 | zout.closeEntry();
970 | fin.close();
971 | }
972 | if (zout != null) {
973 | zout.close();
974 | }
975 | if (deleteAfterZip) {
976 | for (File file : inputs) {
977 | delete(file);
978 | }
979 | }
980 | }
981 |
982 | public static void main(String[] args) throws Exception {
983 | File zipFile = new File("/home/tungmeo/test/test.zip");
984 |
985 | ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
986 | for (ZipEntry entry = zipInputStream.getNextEntry(); entry != null; entry = zipInputStream.getNextEntry()) {
987 | if (!entry.isDirectory()) {
988 | System.out.println("File:" + entry.getName());
989 | BufferedReader reader = new BufferedReader(new InputStreamReader(zipInputStream));
990 | for (String line = reader.readLine(); line != null; line = reader.readLine()) {
991 | System.out.println(line);
992 | }
993 | System.out.println();
994 | }
995 | }
996 | zipInputStream.closeEntry();
997 | zipInputStream.close();
998 | }
999 |
1000 | }
1001 |
--------------------------------------------------------------------------------
/src/java/utils/JsonUtils.java:
--------------------------------------------------------------------------------
1 | package utils;
2 |
3 | import com.google.gson.Gson;
4 | import com.google.gson.GsonBuilder;
5 | import com.google.gson.JsonSyntaxException;
6 | import com.google.gson.stream.JsonReader;
7 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
8 | import org.jetbrains.annotations.NotNull;
9 |
10 | import java.io.BufferedWriter;
11 | import java.io.File;
12 | import java.io.IOException;
13 | import java.io.Reader;
14 | import java.util.Arrays;
15 | import java.util.List;
16 |
17 | /**
18 | * Created on 14/11/2015.
19 | */
20 | public class JsonUtils {
21 | private static final Gson GSON_INLINE = new Gson();
22 | private static final Gson GSON_PRETTY = new GsonBuilder().setPrettyPrinting().create();
23 |
24 | public static String toJsonInline(Object obj) {
25 | return GSON_INLINE.toJson(obj);
26 | }
27 |
28 | public static String toJsonPretty(Object obj) {
29 | return GSON_PRETTY.toJson(obj);
30 | }
31 |
32 | public static T fromJson(String json, Class classOfT) throws JsonSyntaxException {
33 | return GSON_PRETTY.fromJson(json, classOfT);
34 | }
35 |
36 | public static JsonArrayReader openJsonArrayReader(
37 | final File jsonArrayFile, Class classOfT) throws IOException {
38 | return new JsonArrayReader<>(jsonArrayFile, classOfT);
39 | }
40 |
41 | public static JsonArrayWriter openJsonArrayWriter(final File jsonArrayFile) throws IOException {
42 | return new JsonArrayWriter(jsonArrayFile);
43 | }
44 |
45 | public static void writeArray(Iterable list, final File jsonArrayFile) throws IOException {
46 | JsonArrayWriter writer = openJsonArrayWriter(jsonArrayFile);
47 | try {
48 | writer.writeAll(list);
49 | } finally {
50 | writer.close();
51 | }
52 | }
53 |
54 | public static void writeArray(T[] list, final File jsonArrayFile) throws IOException {
55 | writeArray(Arrays.asList(list), jsonArrayFile);
56 | }
57 |
58 | public static class JsonArrayReader {
59 | private final JsonReader reader;
60 | private final Gson gson = new Gson();
61 | private final Class classOfT;
62 | private boolean finish = false;
63 |
64 | public JsonArrayReader(File jsonArrayFile, Class classOfT) throws IOException {
65 | this(FileUtils.openBufferReader(jsonArrayFile), classOfT);
66 | }
67 |
68 | public JsonArrayReader(Reader reader, Class classOfT) throws IOException {
69 | this.reader = new JsonReader(reader);
70 | this.classOfT = classOfT;
71 | this.reader.beginArray();
72 | }
73 |
74 | public boolean hasNext() throws IOException {
75 | return reader == null ? false : reader.hasNext();
76 | }
77 |
78 | public T next() throws IOException {
79 | if (finish) {
80 | return null;
81 | } else if (reader.hasNext()) {
82 | return gson.fromJson(reader, classOfT);
83 | } else {
84 | finish = true;
85 | reader.endArray();
86 | }
87 | return null;
88 | }
89 |
90 | public List readAll() throws IOException {
91 | try {
92 | List result = new ObjectArrayList<>();
93 | for (T val = next(); val != null; val = next()) {
94 | result.add(val);
95 | }
96 | return result;
97 | } finally {
98 | reader.close();
99 | }
100 | }
101 |
102 | public void close() throws IOException {
103 | reader.close();
104 | }
105 | }
106 |
107 | public static class JsonArrayWriter {
108 | private final BufferedWriter writer;
109 | private int lines = 0;
110 | private boolean isClosed = false;
111 |
112 | private JsonArrayWriter(File file) throws IOException {
113 | this.writer = FileUtils.openWriter(file);
114 | this.writer.write("[");
115 | }
116 |
117 | public synchronized void write(@NotNull Object object) throws IOException {
118 | if (lines > 0) {
119 | writer.write(",");
120 | writer.newLine();
121 | }
122 | writer.write(toJsonInline(object));
123 | lines++;
124 | }
125 |
126 | public synchronized void writeAll(Iterable list) throws IOException {
127 | for (T object : list) {
128 | write(object);
129 | }
130 | }
131 |
132 | public synchronized void writeAll(T[] list) throws IOException {
133 | for (T object : list) {
134 | write(object);
135 | }
136 | }
137 |
138 | public void close() throws IOException {
139 | if (isClosed) {
140 | return;
141 | } else {
142 | synchronized (this) {
143 | if (!isClosed) {
144 | this.writer.write("]");
145 | this.writer.close();
146 | isClosed = true;
147 | }
148 | }
149 | }
150 |
151 | }
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/src/java/utils/RandomUtils.java:
--------------------------------------------------------------------------------
1 | package utils;
2 |
3 | import java.util.UUID;
4 |
5 | /**
6 | * Created on 12/11/2015.
7 | */
8 | public final class RandomUtils {
9 | private RandomUtils() {
10 | }
11 |
12 | public static String randomUUID() {
13 | return UUID.randomUUID().toString();
14 | }
15 |
16 | private static String randomByCut(String rand, int maxChars) {
17 | if (rand.length() > maxChars) {
18 | rand = rand.substring(0, maxChars);
19 | }
20 | return rand;
21 | }
22 |
23 | public static String randomUUID(int maxChars) {
24 | String rand = randomUUID();
25 | return randomByCut(rand, maxChars);
26 | }
27 |
28 | public static String randomShortUUID() {
29 | String rand = randomUUID();
30 | int id = rand.lastIndexOf('-');
31 | if (id > 0 && id < rand.length() - 1) {
32 | return rand.substring(id + 1);
33 | }
34 | return rand;
35 | }
36 |
37 | public static String randomUserId() {
38 | String rand = randomUUID();
39 | StringBuilder uid = new StringBuilder(rand.length());
40 | for (int i = 0; i < rand.length(); i++) {
41 | if (rand.charAt(i) != '-') {
42 | uid.append(rand.charAt(i));
43 | }
44 | }
45 | return uid.toString();
46 | }
47 |
48 | public static void main(String[] args) throws Exception {
49 | System.out.println(randomUserId());
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/java/utils/RuntimeUtils.java:
--------------------------------------------------------------------------------
1 | package utils;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.InputStreamReader;
5 |
6 | public class RuntimeUtils {
7 |
8 | public static String executeLinuxDebianCommand(String[] command) throws Exception {
9 | StringBuilder output = new StringBuilder();
10 | Process p = Runtime.getRuntime().exec(command);
11 | // p.waitFor(); // use this might cause a deadlock
12 | BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
13 | String line;
14 | while ((line = reader.readLine()) != null) {
15 | output.append(line).append("\n");
16 | }
17 | return output.toString();
18 | }
19 |
20 | public static int getAvailableCores() {
21 | return Runtime.getRuntime().availableProcessors();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/java/utils/StringUtils.java:
--------------------------------------------------------------------------------
1 | package utils;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 |
6 | /**
7 | * @author tunght
8 | */
9 | public class StringUtils {
10 |
11 | private static final int OBJ_HEADER;
12 | private static final int ARR_HEADER;
13 | private static final int INT_FIELDS = 12;
14 | private static final int OBJ_REF;
15 | private static final int OBJ_OVERHEAD;
16 | private static final boolean IS_64_BIT_JVM;
17 |
18 | /**
19 | * Class initializations.
20 | */
21 | static {
22 | // By default we assume 64 bit JVM
23 | // (defensive approach since we will get
24 | // larger estimations in case we are not sure)
25 |
26 | boolean is64BitsJVM = true;
27 | // check the system property "sun.arch.data.model"
28 | // not very safe, as it might not work for all JVM implementations
29 | // nevertheless the worst thing that might happen is that the JVM is
30 | // 32bit
31 | // but we assume its 64bit, so we will be counting a few extra bytes per
32 | // string object
33 | // no harm done here since this is just an approximation.
34 | String arch = System.getProperty("sun.arch.data.model");
35 | if (arch != null) {
36 | if (arch.indexOf("32") != -1) {
37 | // If exists and is 32 bit then we assume a 32bit JVM
38 | is64BitsJVM = false;
39 | }
40 | }
41 | IS_64_BIT_JVM = is64BitsJVM;
42 | // The sizes below are a bit rough as we don't take into account
43 | // advanced JVM options such as compressed oops
44 | // however if our calculation is not accurate it'll be a bit over
45 | // so there is no danger of an out of memory error because of this.
46 | OBJ_HEADER = IS_64_BIT_JVM ? 16 : 8;
47 | ARR_HEADER = IS_64_BIT_JVM ? 24 : 12;
48 | OBJ_REF = IS_64_BIT_JVM ? 8 : 4;
49 | OBJ_OVERHEAD = OBJ_HEADER + INT_FIELDS + OBJ_REF + ARR_HEADER;
50 | }
51 |
52 | /**
53 | * Estimates the size of a {@link String} object in bytes.
54 | *
55 | * @param s The string to estimate memory footprint.
56 | * @return The estimated size in bytes.
57 | */
58 | public static long estimatedSizeOf(String s) {
59 | return (s.length() * 2) + OBJ_OVERHEAD;
60 | }
61 |
62 | /*
63 | * don't count reference point to string
64 | */
65 | public static long size(String str) {
66 | return (str.length() * 2) + OBJ_OVERHEAD;
67 | }
68 |
69 | public static String getStringFromTokens(String[] tokens, int from, int to) {
70 | return getStringFromTokens(tokens, from, to, ' ');
71 | }
72 |
73 | public static String getStringFromTokens(Iterable tokens) {
74 | return getStringFromTokens(tokens, " ");
75 | }
76 |
77 | public static String getStringFromTokens(Iterable tokens, String split) {
78 | StringBuilder result = new StringBuilder();
79 | boolean notFirst = false;
80 | for (String token : tokens) {
81 | if (notFirst) {
82 | result.append(split);
83 | }
84 | result.append(token);
85 | notFirst = true;
86 | }
87 | return result.toString();
88 | }
89 |
90 | public static String getStringFromTokens(String[] tokens) {
91 | return getStringFromTokens(tokens, 0, tokens.length, ' ');
92 | }
93 |
94 | public static String getStringFromTokens(String[] tokens, String split) {
95 | return getStringFromTokens(tokens, 0, tokens.length, split);
96 | }
97 |
98 | public static String getStringFromTokens(String[] tokens, int from, int to, String split) {
99 | StringBuilder rez = new StringBuilder();
100 | for (int i = from; i < to; i++) {
101 | if (rez.length() > 0) {
102 | rez.append(split);
103 | }
104 | rez.append(tokens[i]);
105 | }
106 | return rez.toString();
107 | }
108 |
109 | public static String getStringFromTokens(String[] tokens, int from, int to, char split) {
110 | StringBuilder rez = new StringBuilder();
111 | for (int i = from; i < to; i++) {
112 | if (rez.length() > 0) {
113 | rez.append(split);
114 | }
115 | rez.append(tokens[i]);
116 | }
117 | return rez.toString();
118 | }
119 |
120 | public static int indexOf(String[] tokens, String token) {
121 | int index = -1;
122 | if (tokens != null) {
123 | for (int i = 0; i < tokens.length; i++) {
124 | if (token == null) {
125 | if (tokens[i] == null) {
126 | index = i;
127 | break;
128 | }
129 | } else if (token.equals(tokens[i])) {
130 | index = i;
131 | break;
132 | }
133 | }
134 | }
135 | return index;
136 | }
137 |
138 | public static boolean contains(String[] tokens, String token) {
139 | return indexOf(tokens, token) >= 0;
140 | }
141 |
142 | public static String getStringFromTokens(String[] tokens, int from) {
143 | return getStringFromTokens(tokens, from, tokens.length);
144 | }
145 |
146 | private static String substring(String str, int from, int to) {
147 | if (from == to) {
148 | return "";
149 | }
150 | return str.substring(from, to);
151 | }
152 |
153 | public static String[] split(String str, char delim, int max) {
154 | if (max <= 0) {
155 | return splitTrim(str, delim);
156 | }
157 | String[] tokens = new String[max];
158 | int id = 0;
159 | int start = 0;
160 | int index = str.indexOf(delim);
161 | while (index >= 0 && id < max) {
162 | if (id + 1 == max) {
163 | index = str.length();
164 | }
165 | if (start <= index) {
166 | tokens[id++] = substring(str, start, index);
167 | }
168 | start = index + 1;
169 | index = str.indexOf(delim, start);
170 | }
171 | if (start <= str.length() && id < max) {
172 | tokens[id++] = substring(str, start, str.length());
173 | }
174 | if (id < max) {
175 | return Arrays.copyOf(tokens, id);
176 | }
177 | return tokens;
178 | }
179 |
180 | public static String[] split(String str, String delim, int max) {
181 | String[] tokens = new String[max];
182 | int id = 0;
183 | int start = 0;
184 | int index = str.indexOf(delim);
185 | while (index >= 0 && id < max) {
186 | if (id + 1 == max) {
187 | index = str.length();
188 | }
189 | if (start <= index) {
190 | tokens[id++] = substring(str, start, index);
191 | }
192 | start = index + delim.length();
193 | index = str.indexOf(delim, start);
194 | }
195 | if (start <= str.length() && id < max) {
196 | tokens[id++] = substring(str, start, str.length());
197 | }
198 | if (id < max) {
199 | return Arrays.copyOf(tokens, id);
200 | }
201 | return tokens;
202 | }
203 |
204 | public static String[] splitTrim(String str, char delim, int max) {
205 | if (max <= 0) {
206 | return splitTrim(str, delim);
207 | }
208 | String[] tokens = new String[max];
209 | int id = 0;
210 | int start = 0;
211 | int index = str.indexOf(delim);
212 | while (index >= 0 && id < max) {
213 | if (id + 1 == max) {
214 | index = str.length();
215 | }
216 | if (start < index) {
217 | tokens[id++] = substring(str, start, index);
218 | }
219 | start = index + 1;
220 | index = str.indexOf(delim, start);
221 | }
222 | if (start < str.length() && id < max) {
223 | tokens[id++] = substring(str, start, str.length());
224 | }
225 | if (id < max) {
226 | return Arrays.copyOf(tokens, id);
227 | }
228 | return tokens;
229 | }
230 |
231 | public static String[] splitTrim(String str, String delim, int max) {
232 | if (max <= 0) {
233 | return splitTrim(str, delim);
234 | }
235 | String[] tokens = new String[max];
236 | int id = 0;
237 | int start = 0;
238 | int index = str.indexOf(delim);
239 | while (index >= 0 && id < max) {
240 | if (id + 1 == max) {
241 | index = str.length();
242 | }
243 | if (start < index) {
244 | tokens[id++] = substring(str, start, index);
245 | }
246 | start = index + delim.length();
247 | index = str.indexOf(delim, start);
248 | }
249 | if (start < str.length() && id < max) {
250 | tokens[id++] = substring(str, start, str.length());
251 | }
252 | if (id < max) {
253 | return Arrays.copyOf(tokens, id);
254 | }
255 | return tokens;
256 | }
257 |
258 | public static String[] split(String str, String delim) {
259 | ArrayList rez = new ArrayList<>();
260 | int start = 0;
261 | int index = str.indexOf(delim);
262 | while (index >= 0) {
263 | rez.add(substring(str, start, index));
264 | start = index + delim.length();
265 | index = str.indexOf(delim, start);
266 | }
267 | if (start <= str.length()) {
268 | rez.add(substring(str, start, str.length()));
269 | }
270 | return rez.toArray(new String[0]);
271 | }
272 |
273 | public static String[] splitTrim(String str, String delim) {
274 | ArrayList rez = new ArrayList<>();
275 | int start = 0;
276 | int index = str.indexOf(delim);
277 | while (index >= 0) {
278 | if (start < index) {
279 | rez.add(substring(str, start, index));
280 | }
281 | start = index + delim.length();
282 | index = str.indexOf(delim, start);
283 | }
284 | if (start < str.length()) {
285 | rez.add(substring(str, start, str.length()));
286 | }
287 | return rez.toArray(new String[0]);
288 | }
289 |
290 | public static String[] split(String str, char delim) {
291 | ArrayList rez = new ArrayList<>();
292 | int start = 0;
293 | int index = str.indexOf(delim);
294 | while (index >= 0) {
295 | rez.add(substring(str, start, index));
296 | start = index + 1;
297 | index = str.indexOf(delim, start);
298 | }
299 | if (start <= str.length()) {
300 | rez.add(substring(str, start, str.length()));
301 | }
302 | return rez.toArray(new String[0]);
303 | }
304 |
305 | public static String[] splitTrim(String str, char delim) {
306 | ArrayList rez = new ArrayList<>();
307 | int start = 0;
308 | int index = str.indexOf(delim);
309 | while (index >= 0) {
310 | if (start < index) {
311 | rez.add(substring(str, start, index));
312 | }
313 | start = index + 1;
314 | index = str.indexOf(delim, start);
315 | }
316 | if (start < str.length()) {
317 | rez.add(substring(str, start, str.length()));
318 | }
319 | return rez.toArray(new String[0]);
320 | }
321 |
322 | public static String getStringFromObjects(Object delim, Object[] objs) {
323 | StringBuilder rez = new StringBuilder();
324 | boolean first = true;
325 | for (Object obj : objs) {
326 | if (first) {
327 | first = false;
328 | } else {
329 | rez.append(delim.toString());
330 | }
331 | rez.append(obj.toString());
332 | }
333 | return rez.toString();
334 | }
335 |
336 | public static String getField(final String line, final String sparator,
337 | final int index, final boolean trim) {
338 | if (index <= 0) {
339 | return null;
340 | }
341 | int id = 0, len = line.length();
342 | if (trim) {
343 | while (line.startsWith(sparator, id)) {
344 | id += sparator.length();
345 | }
346 | while (line.startsWith(sparator, len - sparator.length())) {
347 | len = len - sparator.length();
348 | }
349 | }
350 | int count = 0, max = len;
351 | int last = id;
352 | while (count < index) {
353 | count++;
354 | id = line.indexOf(sparator, last);
355 | if (id > 0 && trim) {
356 | int next = id + sparator.length();
357 | while (next <= len && line.startsWith(sparator, next)) {
358 | id = next;
359 | next += sparator.length();
360 | }
361 | }
362 | if (id < 0 || id >= max) {
363 | if (count == index) {
364 | id = len;
365 | }
366 | break;
367 | } else if (count < index) {
368 | id += sparator.length();
369 | last = id;
370 | }
371 | }
372 | String result = id >= last && count == index ? line.substring(last, id) : null;
373 | return result;
374 | }
375 |
376 | public static void main(String[] args) {
377 | }
378 |
379 | }
380 |
--------------------------------------------------------------------------------
/src/java/utils/TimestampUtils.java:
--------------------------------------------------------------------------------
1 | package utils;
2 |
3 | import org.joda.time.format.DateTimeFormat;
4 | import org.joda.time.format.DateTimeFormatter;
5 |
6 | import java.text.DateFormat;
7 | import java.text.SimpleDateFormat;
8 | import java.util.Date;
9 | import java.util.TimeZone;
10 |
11 | /**
12 | * Methods for dealing with timestamps
13 | */
14 | public class TimestampUtils {
15 |
16 | private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormat.forPattern("yyyy_MM_dd");
17 | private static final TimeZone TIME_ZONE = TimeZone.getDefault();
18 |
19 | public static final long getTimeZoneOffset(long date, long unit) {
20 | return TIME_ZONE.getOffset(date) / unit;
21 | }
22 |
23 | public static final long getTimeZoneOffset(long date) {
24 | return TIME_ZONE.getOffset(date);
25 | }
26 |
27 | public static String getIsoWithoutUtcRootZone() {
28 | return getIsoWithoutUtcRootZone(System.currentTimeMillis());
29 | }
30 |
31 | public static String getIsoWithoutUtcRootZone(long timestamp) {
32 | Date now = new Date(timestamp);
33 | return getIsoWithoutUtcRootZone(now);
34 | }
35 |
36 | private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
37 |
38 | static {
39 | dateFormat.setTimeZone(TimeZone.getTimeZone("England/London"));
40 | }
41 |
42 | private static String getIsoWithoutUtcRootZone(Date date) {
43 | return dateFormat.format(date);
44 | }
45 |
46 | public static DateTimeFormatter getDateTimeFormat(String pattern) {
47 | return DateTimeFormat.forPattern(pattern);
48 | }
49 |
50 | private TimestampUtils() {
51 | }
52 |
53 | public static void main(String[] args) throws Exception {
54 | System.out.println(getIsoWithoutUtcRootZone());
55 | System.out.println(getTimeZoneOffset(System.currentTimeMillis()) / (3600 * 1000));
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/java/utils/Utils.java:
--------------------------------------------------------------------------------
1 | package utils;
2 |
3 | import java.io.UnsupportedEncodingException;
4 | import java.net.InetAddress;
5 | import java.net.URLDecoder;
6 | import java.net.UnknownHostException;
7 | import java.text.DateFormat;
8 | import java.text.SimpleDateFormat;
9 | import java.util.*;
10 | import java.util.concurrent.TimeUnit;
11 |
12 | public class Utils {
13 | public static String round(double d, int num) {
14 | return String.format(Locale.US, "%." + num + "f", d);
15 | }
16 |
17 | public static String round(double d) {
18 | return String.format(Locale.US, "%.3f", d);
19 | }
20 |
21 | public static double min(double a, double b) {
22 | return (a < b) ? a : b;
23 | }
24 |
25 | public static long double2long(double d) {
26 | if (d > Long.MAX_VALUE) {
27 | return Long.MAX_VALUE;
28 | }
29 | if (d < Long.MIN_VALUE) {
30 | return Long.MIN_VALUE;
31 | }
32 | return (long) d;
33 | }
34 |
35 | public static int double2int(double d) {
36 | if (d > Integer.MAX_VALUE) {
37 | return Integer.MAX_VALUE;
38 | }
39 | if (d < Integer.MIN_VALUE) {
40 | return Integer.MIN_VALUE;
41 | }
42 | return (int) d;
43 | }
44 |
45 | /**
46 | * different in hours between two timestamp
47 | * time must be in format of DATE_FORMAT (yyyy_MM_dd_HH_mm)
48 | * @param time1
49 | * @param time2
50 | * @return
51 | */
52 | public static long hourdiff(String time1, String time2) {
53 | try {
54 | Date date1 = DATE_FORMAT.parse(time1);
55 | Date date2 = DATE_FORMAT.parse(time2);
56 |
57 | long diff = date2.getTime() - date1.getTime();
58 | return TimeUnit.MILLISECONDS.toHours(diff);
59 | } catch(Exception e) {
60 | e.printStackTrace();
61 | }
62 | return 0;
63 | }
64 |
65 | public static long hourElapsed(String time) {
66 | try {
67 | String current = DATE_FORMAT.format(Calendar.getInstance().getTime());
68 | return hourdiff(time, current);
69 | } catch (Exception e) {
70 | e.printStackTrace();
71 | }
72 | return 0;
73 | }
74 |
75 | public static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
76 |
77 | /*
78 | * get name with date specific as suffix
79 | */
80 | public static String getNameWithDateSuffix(String name) {
81 | return name + "_" + DATE_FORMAT.format(Calendar.getInstance().getTime());
82 | }
83 |
84 | /**
85 | * get current time
86 | * @return formated String of current time
87 | */
88 | public static String getCurrentDate() {
89 | return DATE_FORMAT.format(Calendar.getInstance().getTime());
90 | }
91 |
92 | /**
93 | * Check if date1 is after date2 or not
94 | * @param date1
95 | * @param date2
96 | * @return
97 | */
98 | public static boolean isAfter(String date1, String date2) {
99 | try {
100 | Date d1 = DATE_FORMAT.parse(date1);
101 | Date d2 = DATE_FORMAT.parse(date2);
102 |
103 | return d1.after(d2);
104 | } catch(Exception e) {
105 | e.printStackTrace();
106 | return false;
107 | }
108 | }
109 |
110 |
111 | /*
112 | * get name with date specific as suffix
113 | */
114 | public static String getNameWithDateSuffix(String name, boolean zip) {
115 | if (!zip) {
116 | return getNameWithDateSuffix(name);
117 | }
118 | return name + "_" + DATE_FORMAT.format(Calendar.getInstance().getTime()) + ".gz";
119 | }
120 |
121 | public static String getStringFromTokens(String[] tokens, int start, int end, StringBuilder cache) {
122 | if (cache == null) {
123 | cache = new StringBuilder();
124 | } else {
125 | cache.setLength(0);
126 | }
127 | for (int i = start; i < end; i++) {
128 | if (cache.length() > 0) {
129 | cache.append(' ');
130 | }
131 | cache.append(tokens[i]);
132 | }
133 | return cache.toString();
134 | }
135 |
136 | public static String getStringFromTokens(String[] tokens, int start, int end) {
137 | return getStringFromTokens(tokens, start, end, null);
138 | }
139 |
140 | public static void toJson(String s, StringBuilder result) {
141 | for (int i = 0; i < s.length(); i++) {
142 | char c = s.charAt(i);
143 | switch (c) {
144 | case '"':
145 | case '/':
146 | case '\\':
147 | result.append('\\').append(c);
148 | break;
149 | case '\b':
150 | result.append("\\b");
151 | break;
152 | case '\f':
153 | result.append("\\f");
154 | break;
155 | case '\n':
156 | result.append("\\n");
157 | break;
158 | case '\r':
159 | result.append("\\r");
160 | break;
161 | case '\t':
162 | result.append("\\t");
163 | break;
164 | default:
165 | result.append(c);
166 | }
167 | }
168 | }
169 |
170 | public static String toJson(String s) {
171 | StringBuilder result = new StringBuilder(s.length() + 2);
172 | toJson(s, result);
173 | return result.toString();
174 | }
175 |
176 | /**
177 | * rewrite String.startswith for speeding up
178 | */
179 | private static boolean startsWith(String s, String prefix) {
180 | if (s.length() < prefix.length()) {
181 | return false;
182 | }
183 | for (int i = 0; i < prefix.length(); i++) {
184 | if (prefix.charAt(i) != s.charAt(i)) {
185 | return false;
186 | }
187 | }
188 | return true;
189 | }
190 |
191 | /**
192 | * www[0-9]*.site -> site
193 | */
194 | public static String normalize(String site) {
195 | site = site.toLowerCase();
196 | if (startsWith(site, "www")) {
197 | int pos = 3;
198 | while (pos < site.length()
199 | && site.charAt(pos) >= '0' && site.charAt(pos) <= '9') {
200 | pos++;
201 | }
202 | if (pos + 1 < site.length() && site.charAt(pos) == '.') {
203 | return site.substring(pos + 1);
204 | }
205 | }
206 | return site;
207 | }
208 |
209 | /**
210 | * check hash before using default equal function
211 | */
212 | public static boolean equalUsingHash(String str1, String str2) {
213 | if (str1 == null || str2 == null) {
214 | return false;
215 | }
216 | if (str1.hashCode() != str2.hashCode()) {
217 | return false;
218 | }
219 | return str1.equals(str2);
220 | }
221 |
222 | /*
223 | * get real local host name of running code
224 | * ex: searcher1g.dev.itim.vn
225 | */
226 | public static String getLocalHostName() throws UnknownHostException {
227 | return InetAddress.getLocalHost().getCanonicalHostName();
228 | }
229 |
230 | public static String getLocalHostName(String defaultName) {
231 | try {
232 | return InetAddress.getLocalHost().getCanonicalHostName();
233 | } catch (UnknownHostException e) {
234 | return defaultName;
235 | }
236 | }
237 |
238 | /*
239 | * get available cores from current OS
240 | * use this value for spawning a suitable number of threads (as average, best number is available cores)
241 | */
242 | public static int getAvailableCores() {
243 | return Runtime.getRuntime().availableProcessors();
244 | }
245 |
246 | /*
247 | * get free heap memory of JVM
248 | */
249 | public static long getFreeMem() {
250 | return Runtime.getRuntime().freeMemory();
251 | }
252 |
253 | /*
254 | * get map between param and value base on format "param1=value1¶m2=value2..."
255 | * note that requestPath has url encoding (ansii)
256 | */
257 | public static Map getParamsFromRequestPath(String requestPath, int startIndex)
258 | throws UnexpectedResultException, UnsupportedEncodingException {
259 | Map params = new HashMap<>();
260 | StringBuilder param = new StringBuilder();
261 | int lastIndex = requestPath.length() - 1;
262 | for (int i = startIndex; i <= lastIndex; i++) {
263 | if (requestPath.charAt(i) == '&') {
264 | if (param.length() == 0) {
265 | continue;
266 | }
267 | String pair = param.toString();
268 | int index = pair.indexOf('=');
269 | if (index < 0) {
270 | throw new UnexpectedResultException("invalid param '" + pair + "' of request '" + requestPath + "'");
271 | }
272 | params.put(pair.substring(0, index), URLDecoder.decode(pair.substring(index + 1), "UTF-8"));
273 | param.setLength(0);
274 | } else {
275 | param.append(requestPath.charAt(i));
276 | if (i == lastIndex) {
277 | String pair = param.toString();
278 | int index = pair.indexOf('=');
279 | if (index < 0) {
280 | throw new UnexpectedResultException("invalid param '" + pair + "' of request '" + requestPath + "'");
281 | }
282 | params.put(pair.substring(0, index), URLDecoder.decode(pair.substring(index + 1), "UTF-8"));
283 | }
284 | }
285 | }
286 | return params;
287 | }
288 |
289 |
290 | public static List addAllStringArray(List source, String[] array) {
291 | for(int i=0; i T instantiate(final String className, final Class type){
308 | try{
309 | return type.cast(Class.forName(className).newInstance());
310 | } catch(final InstantiationException e){
311 | throw new IllegalStateException(e);
312 | } catch(final IllegalAccessException e){
313 | throw new IllegalStateException(e);
314 | } catch(final ClassNotFoundException e){
315 | throw new IllegalStateException(e);
316 | }
317 | }
318 | }
319 |
--------------------------------------------------------------------------------
/src/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | groupId
8 | recsys4all
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | com.github.fommil.netlib
14 | all
15 | 1.1.2
16 | pom
17 |
18 |
19 | com.googlecode.matrix-toolkits-java
20 | mtj
21 | 1.0.1
22 |
23 |
24 | com.googlecode.efficient-java-matrix-library
25 | core
26 | 0.26
27 |
28 |
29 | com.googlecode.efficient-java-matrix-library
30 | equation
31 | 0.26
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/src.eml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------