Skip to content

Commit

Permalink
Code: Implement scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
duangsuse committed Apr 7, 2019
1 parent 2c30241 commit 35117f2
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 8 deletions.
18 changes: 17 additions & 1 deletion Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.duangsuse.telegramscanner;

import org.duangsuse.telegramscanner.model.Message;
import org.duangsuse.telegramscanner.scanner.Scanner;
import org.duangsuse.telegramscanner.scanner.Utf8LineInputStream;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;

import java.util.List;

/**
* Application main class
Expand All @@ -27,7 +32,7 @@ private Main() {}
/**
* Standard input
*/
private static PrintStream err = System.err;
public static PrintStream err = System.err;

/**
* Program entrance
Expand All @@ -36,7 +41,18 @@ private Main() {}
*/
public static void main(String... args) {
err.print("TelegramScanner version "); err.println(VERSION);
List<String> argList = Arrays.asList(args);

if (argList.contains("-test"))
testInput();

for (Message<String> stringMessage : new Scanner(System.in)) {
out.print(stringMessage.toString());
}
}

@SuppressWarnings("WeakerAccess")
public static void testInput() {
Utf8LineInputStream input = new Utf8LineInputStream(System.in);
String line = "";

Expand Down
5 changes: 4 additions & 1 deletion helper/Strings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.duangsuse.telegramscanner.helper;

import org.jetbrains.annotations.Contract;

/**
* String helper program
*/
Expand All @@ -11,8 +13,9 @@ public final class Strings {
* @param str target string
* @return if str.length greater than n, then sub-sequence str, else return str
*/
@Contract("_, null -> !null")
public static String take(int n, String str) {
if (n == 0 || str.length() == 0)
if (str == null || n == 0 || str.length() == 0)
return "";

if (str.length() > n)
Expand Down
18 changes: 15 additions & 3 deletions model/Message.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.duangsuse.telegramscanner.model;

import org.duangsuse.telegramscanner.helper.Strings;
import org.duangsuse.telegramscanner.sourcemanager.Identifiable;

import java.util.Collection;
import java.util.LinkedList;
Expand All @@ -17,7 +18,7 @@
* <li>hashTags
* </ul>
*/
public class Message<T> {
public class Message<T> implements Identifiable {
private MessageHeaderType headerType = MessageHeaderType.NORMAL;
/**
* Message header (name, publishedAt)
Expand Down Expand Up @@ -47,15 +48,21 @@ public class Message<T> {
*/
public Message() {}

public Message(MessageHead head, MessageHeaderType type, T extRef) {
this.header = head;
this.headerType = type;
this.messageExtRef = extRef;
}

@Override
public String toString() {
// count links and hashtags
final StringBuilder desc = new StringBuilder();
if (links.size() != 0) desc.append(links.size()).append(" links");
if (links.size() != 0) desc.append(links.size()).append(" links, ");
if (hashtags.size() != 0) desc.append(hashtags.size()).append(" tags");

final String fmt = "Message{Hd%s, Bd%s, ext=%s}[%s](%s..., %s)";
return String.format(fmt, headerType, bodyType, messageExtRef.toString(), header, Strings.take(BODY_PREVIEW_LEN, messageBody), desc);
return String.format(fmt, headerType, bodyType, messageExtRef.toString(), header.toString(), Strings.take(BODY_PREVIEW_LEN, messageBody), desc);
}

@Override
Expand Down Expand Up @@ -87,6 +94,11 @@ public int hashCode() {
return result;
}

@Override
public int getIdentity() {
return System.identityHashCode(this);
}

public MessageHeaderType getHeaderType() {
return headerType;
}
Expand Down
2 changes: 1 addition & 1 deletion model/MessageHeaderType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum MessageHeaderType {
/**
* Reply to message
*/
RELPY,
REPLY,
/**
* Forwarded message
*/
Expand Down
Loading

0 comments on commit 35117f2

Please sign in to comment.