Java StringBuilder.length Examples

Java StringBuilder.length - 30 examples found. These are the top rated real world Java examples of StringBuilder.length extracted from open source projects. You can rate examples to help us improve the quality of examples.
  public String getPositionAndAffiliationString(int limit, int additionalLength, String tail) {
    if (null == tail) {
      tail = "...";
    }
    StringBuilder sb = new StringBuilder();
    if (!Strings.isEmpty(this.get_sPosition())) {
      sb.append(this.get_sPosition());
    }
    if (!Strings.isEmpty(this.get_sPosition()) && !Strings.isEmpty(this.get_sAffiliation())) {
      sb.append(", ");
    }
    if (!Strings.isEmpty(this.get_sAffiliation())) {
      sb.append(this.get_sAffiliation());
    }
    int max = limit - additionalLength;

    // capitalize
    if (sb.length() > 0) {
      sb.replace(0, 1, new Character(sb.charAt(0)).toString().toUpperCase());
    }

    String result = null;
    if (sb.length() > max) {
      result = sb.substring(0, max) + tail;
    } else {
      result = sb.toString();
    }
    return result;
  }
Example #2
0
  /**
   * Break on commas, except those inside quotes, e.g.: size(300, 200, PDF, "output,weirdname.pdf");
   * No special handling implemented for escaped (\") quotes.
   */
  private static StringList breakCommas(String contents) {
    StringList outgoing = new StringList();

    boolean insideQuote = false;
    // The current word being read
    StringBuilder current = new StringBuilder();
    char[] chars = contents.toCharArray();
    for (int i = 0; i < chars.length; i++) {
      char c = chars[i];
      if (insideQuote) {
        current.append(c);
        if (c == '\"') {
          insideQuote = false;
        }
      } else {
        if (c == ',') {
          if (current.length() != 0) {
            outgoing.append(current.toString());
            current.setLength(0);
          }
        } else {
          current.append(c);
          if (c == '\"') {
            insideQuote = true;
          }
        }
      }
    }
    if (current.length() != 0) {
      outgoing.append(current.toString());
    }
    return outgoing;
  }
Example #3
0
  public static String prepareFilters(final SearchRequest req) {
    final StringBuilder builder = new StringBuilder(Query.FETCH_IMAGES.toString());

    // Add date filters
    builder.append(" where startedAt >= ? and endedAt <= ? ");

    if (req.getUserList() != null && req.getUserList().size() > 0) {
      // Add user name filters
      builder.append(" and userid in (");
      int count = req.getUserList().size();
      while (count-- > 0) builder.append("?,");
      builder.setLength(builder.length() - 1);
      builder.append(") ");
    }

    // Add location filters
    if (req.getLocationList() != null && req.getLocationList().size() > 0) {
      builder.append(" and location in (");
      int count = req.getLocationList().size();
      while (count-- > 0) builder.append("?,");
      builder.setLength(builder.length() - 1);
      builder.append(") ");
    }

    builder.append(" order by userid,startedAt;");

    return builder.toString();
  }
Example #4
0
  static Integer helper(int i, StringBuilder sb, String s) {
    if (i == s.length()) {
      return decode(sb.toString());
    }

    if (!Character.isLetter(s.charAt(i))) {
      sb.append(s.charAt(i));
      Integer result = helper(i + 1, sb, s);
      if (result != null) {
        return result;
      }
    } else {
      char ch = s.charAt(i);
      sb.append(Character.toUpperCase(ch));
      Integer result = helper(i + 1, sb, s);
      if (result != null) {
        return result;
      }
      sb.deleteCharAt(sb.length() - 1);

      sb.append(Character.toLowerCase(ch));
      result = helper(i + 1, sb, s);
      if (result != null) {
        return result;
      }
      sb.deleteCharAt(sb.length() - 1);
    }
    return null;
  }
 /**
  * Append a time to the string builder.
  *
  * @param buff the target string builder
  * @param nanos the time in nanoseconds
  * @param alwaysAddMillis whether to always add at least ".0"
  */
 static void appendTime(StringBuilder buff, long nanos, boolean alwaysAddMillis) {
   if (nanos < 0) {
     buff.append('-');
     nanos = -nanos;
   }
   long ms = nanos / 1000000;
   nanos -= ms * 1000000;
   long s = ms / 1000;
   ms -= s * 1000;
   long m = s / 60;
   s -= m * 60;
   long h = m / 60;
   m -= h * 60;
   StringUtils.appendZeroPadded(buff, 2, h);
   buff.append(':');
   StringUtils.appendZeroPadded(buff, 2, m);
   buff.append(':');
   StringUtils.appendZeroPadded(buff, 2, s);
   if (alwaysAddMillis || ms > 0 || nanos > 0) {
     buff.append('.');
     int start = buff.length();
     StringUtils.appendZeroPadded(buff, 3, ms);
     if (nanos > 0) {
       StringUtils.appendZeroPadded(buff, 6, nanos);
     }
     for (int i = buff.length() - 1; i > start; i--) {
       if (buff.charAt(i) != '0') {
         break;
       }
       buff.deleteCharAt(i);
     }
   }
 }
Example #6
0
  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    StringBuilder selection = new StringBuilder();

    // create temporary copies
    final boolean isFilterFavorites = mIsFilterFavorites;
    final boolean isFilterUnwatched = mIsFilterUnwatched;
    final boolean isFilterUpcoming = mIsFilterUpcoming;
    final boolean isFilterHidden = mIsFilterHidden;

    // restrict to favorites?
    if (isFilterFavorites) {
      selection.append(Shows.FAVORITE).append("=1");
    }

    final long timeInAnHour = System.currentTimeMillis() + DateUtils.HOUR_IN_MILLIS;

    // restrict to shows with a next episode?
    if (isFilterUnwatched) {
      if (selection.length() != 0) {
        selection.append(" AND ");
      }
      selection.append(Shows.NEXTAIRDATEMS).append("!=").append(DBUtils.UNKNOWN_NEXT_AIR_DATE);

      // exclude shows with upcoming next episode
      if (!isFilterUpcoming) {
        selection.append(" AND ").append(Shows.NEXTAIRDATEMS).append("<=").append(timeInAnHour);
      }
    }
    // restrict to shows with an upcoming (yet to air) next episode?
    if (isFilterUpcoming) {
      if (selection.length() != 0) {
        selection.append(" AND ");
      }
      // Display shows upcoming within <limit> days + 1 hour
      int upcomingLimitInDays = AdvancedSettings.getUpcomingLimitInDays(getActivity());
      long latestAirtime = timeInAnHour + upcomingLimitInDays * DateUtils.DAY_IN_MILLIS;

      selection.append(Shows.NEXTAIRDATEMS).append("<=").append(latestAirtime);

      // exclude shows with no upcoming next episode if not filtered for unwatched, too
      if (!isFilterUnwatched) {
        selection.append(" AND ").append(Shows.NEXTAIRDATEMS).append(">=").append(timeInAnHour);
      }
    }

    // special: if hidden filter is disabled, exclude hidden shows
    if (selection.length() != 0) {
      selection.append(" AND ");
    }
    selection.append(Shows.HIDDEN).append(isFilterHidden ? "=1" : "=0");

    return new CursorLoader(
        getActivity(),
        Shows.CONTENT_URI,
        ShowsQuery.PROJECTION,
        selection.toString(),
        null,
        ShowsDistillationSettings.getSortQuery(mSortOrderId, mIsSortFavoritesFirst));
  }