package com.twitter;

import java.text.Normalizer;

/**
 * A class for validating Tweet texts.
 */
public class Validator {
  public static final int MAX_TWEET_LENGTH = 140;

  protected int shortUrlLength = 20;
  protected int shortUrlLengthHttps = 21;

  private Extractor extractor = new Extractor();

  public int getTweetLength(String text) {
    text = Normalizer.normalize(text, Normalizer.Form.NFC);
    int length = text.codePointCount(0, text.length());

    for (Extractor.Entity urlEntity : extractor.extractURLsWithIndices(text)) {
      length += urlEntity.start - urlEntity.end;
      length += urlEntity.value.toLowerCase().startsWith("https://") ? shortUrlLengthHttps : shortUrlLength;
    }

    return length;
  }

  public boolean isValidTweet(String text) {
    if (text == null || text.isEmpty()) {
      return false;
    }

    for (char c : text.toCharArray()) {
      if (c == '\uFFFE' || c == '\uuFEFF' ||   // BOM
          c == '\uFFFF' ||                     // Special
          (c >= '\u202A' && c <= '\u202E')) {  // Direction change
        return false;
      }
    }

    return getTweetLength(text) <= MAX_TWEET_LENGTH;
  }

  public int getShortUrlLength() {
    return shortUrlLength;
  }

  public void setShortUrlLength(int shortUrlLength) {
    this.shortUrlLength = shortUrlLength;
  }

  public int getShortUrlLengthHttps() {
    return shortUrlLengthHttps;
  }

  public void setShortUrlLengthHttps(int shortUrlLengthHttps) {
    this.shortUrlLengthHttps = shortUrlLengthHttps;
  }
}

@@ -202,17 +202,17 @@
 ngth = 2
-0
+2
 ;%0A  prot
@@ -244,17 +244,17 @@
 ttps = 2
-1
+3
 ;%0A%0A  pri

update t.co url lengths