Package lombok

Examples of lombok.val


  /**
   * Returns the last index where the specified not-null element is first found. If the element is not found, this returns -1.
   */
  private static <T> int lastIndexOfNotNull(final T[] values, final T element)
  {
    val count = values.length;
    for (int i = count - 1; i >= 0; i--)
    {
      if (element.equals(values[i]))
        return i;
    }
View Full Code Here


  /**
   * Returns the last index where the specified not-null element is first found. If the element is not found, this returns -1.
   */
  private static <T> int lastIndexOfNotNull(final T[] values, final T element, final Comparator<? super T> comparer)
  {
    val count = values.length;
    for (int i = count - 1; i >= 0; i--)
    {
      if (comparer.compare(element, values[i]) == 0)
        return i;
    }
View Full Code Here

  @Validate
  @SuppressWarnings("unchecked")
  public static <T> T changeType(@NotNull final Object value, @NotNull final Class<T> targetType)
      throws InstantiationException, IllegalAccessException
  {
    val sourceType = value.getClass();

    // check that we're not working with incompatible types
    if (sourceType.isAnnotation() || targetType.isAnnotation())
      throw new IllegalArgumentException("This conversion process does not support annotations.");
    if (sourceType.isArray() || targetType.isArray())
      throw new IllegalArgumentException("This conversion process does not support arrays.");

    // handle conversion to String
    if (targetType.equals(String.class))
      return (T) value.toString();

    // handle parsing from String
    if (sourceType.equals(String.class))
      return (T) changeTypeParseFromString(value, targetType);

    // handle simple casting scenario
    if (sourceType.isAssignableFrom(targetType))
      return targetType.cast(value);

    // handle number conversion
    if (value instanceof Number)
      return (T) changeTypeFromNumber(value, targetType);

    // handle primitive conversion
    PrimitiveType primitiveType = ReflectionUtils.getPrimitiveType(sourceType);
    if (primitiveType != PrimitiveType.NotPrimitive)
      return (T) changeTypeFromPrimitive(value, primitiveType, targetType);

    // handle number-like Character and Boolean conversion (these don't implement Number)
    if (value instanceof Character)
      return (T) changeTypeFromCharacter(value, targetType);
    if (value instanceof Boolean)
      return (T) changeTypeFromBoolean(value, targetType);

    // all attempts have failed
    throw new IllegalArgumentException("The provided object of type '" + sourceType.getSimpleName() + "' could not be converted to '"
        + targetType.getSimpleName());
  }
View Full Code Here

   */
  @Validate
  public static XMLGregorianCalendar toXMLGregorianCalendar(@NotNull final LocalDateTime value)
      throws DatatypeConfigurationException
  {
    val gc = new GregorianCalendar(value.getYear(), value.getMonthOfYear() - 1, value.getDayOfMonth(), value.getHourOfDay(),
        value.getMinuteOfHour(), value.getSecondOfMinute());
    gc.set(Calendar.MILLISECOND, value.getMillisOfSecond());

    return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
  }
View Full Code Here

   * @throws NullPointerException An argument is null.
   */
  @Validate
  public static String toBinary(@NotNull final byte[] bytes)
  {
    val sb = new StringBuilder(bytes.length * 8);
    for (byte b : bytes)
      sb.append(toBinary(b, true));

    return sb.toString();
  }
View Full Code Here

   * minutes become "about 13 hours". The suffix can be used to describe the event's position in time, use e.g. " ago" or " from now"
   */
  @Validate
  public static String toHumanReadable(@NotNull final Duration ts, @NotNull final String suffix)
  {
    val values = new AvlHashtable<Double, String>(Double.class, String.class);

    long totalSeconds = Math.abs(ts.getStandardSeconds());
    final double totalMinutes = Math.round(totalSeconds / 60);
    double totalHours = Math.round(totalMinutes / 60);
    double totalDays = Math.floor(totalHours / 24);
    double totalMonths = Math.floor(totalDays / 30);
    double totalYears = Math.floor(totalMonths / 12);

    values.add(0.75d, "less than a minute");
    values.add(1.5d, "about a minute");
    values.add(45d, String.format("%d minutes", (int) totalMinutes));
    values.add(90d, "about an hour");
    values.add(1440d, String.format("about %d hours", (int) totalHours)); // 60 * 24
    values.add(2880d, "a day"); // 60 * 48
    values.add(43200d, String.format("%d days", (int) totalDays)); // 60 * 24 * 30
    values.add(86400d, "about a month"); // 60 * 24 * 60
    values.add(525600d, String.format("%d months", (int) totalMonths)); // 60 * 24 * 365
    values.add(1051200d, "about a year"); // 60 * 24 * 365 * 2
    values.add(Double.MAX_VALUE, String.format("%d years", (int) totalYears));

    Double key = Linq.first(values.getKeys(), keyGreaterThan(totalMinutes));
    return values.get(key) + suffix;
  }
View Full Code Here

    if (failMode == ScanFailMode.SkipAndCollect)
      if (collectedScanErrors == null)
        throw new IllegalArgumentException("Cannot use " + ScanFailMode.SkipAndCollect
            + " mode without a specified container for accumulating errors.");

    val dir = new File(path);
    if (!dir.exists())
      throw new IOException("The directory cannot be scanned as it does not exist: " + path);
    if (!dir.isDirectory())
      throw new IOException("The path cannot be scanned as it is not a directory: " + path);

    // get full path
    path = dir.getAbsolutePath();

    switch(depthMode)
    {
      case Shallow:
        return scanPathBreadthFirst(path, (inclusionMode == ScanInclusionMode.Files) || (inclusionMode == ScanInclusionMode.All),
View Full Code Here

  private static List<String> scanPathBreadthFirst(String path, boolean includeFiles, boolean includeDirectories,
                                                   ScanHiddenMode hiddenMode, ScanSortMode sortMode, ScanFailMode failMode,
                                                   List<ScanErrorEntry> collectedScanErrors, boolean deepScan)
      throws Exception
  {
    val result = new ArrayList<String>(64);

    // maintain a queue of scanned directories
    val directoryQueue = new LinkedList<String>();
    directoryQueue.addLast(path);

    // check if we need to add self
    if (includeDirectories)
      result.addAll(Linq.toList(Linq.select(hideScanned(new File[] {new File(path)}, hiddenMode, failMode, collectedScanErrors),
          getAbsolutePath())));

    // While there are directories to process
    while (directoryQueue.size() > 0)
    {
      String currDirName = directoryQueue.removeFirst();

      try
      {
        File currDir = new File(currDirName);
        if (currDir.exists() && currDir.isDirectory())
        {
          // check if we need to scan for directories at all
          if (includeDirectories || deepScan)
          {
            // get folders
            File[] dis = filterIn(currDir.listFiles(),
                new HashSet<FileAttribute>(Arrays.asList(new FileAttribute[] {FileAttribute.Directory})));
            dis = hideScanned(dis, hiddenMode, failMode, collectedScanErrors);

            List<String> dirDirs = new ArrayList<String>(64);
            for (File di : dis)
              dirDirs.add(di.getAbsolutePath());

            // sort
            dirDirs = sortScanned(dirDirs, sortMode);

            // check if we need to include directories in results
            if (includeDirectories)
              result.addAll(dirDirs);

            // next directories to process
            if (deepScan)
              for (String dirDir : dirDirs)
                directoryQueue.addLast(dirDir);
          }

          // check if we need to include files in results
          if (includeFiles)
          {
View Full Code Here

   */
  private static List<String> scanPathDepthFirst(String path, boolean includeFiles, boolean includeDirectories, ScanHiddenMode hiddenMode,
                                                 ScanSortMode sortMode, ScanFailMode failMode, List<ScanErrorEntry> collectedScanErrors)
      throws Exception
  {
    val result = new ArrayList<String>(64);

    // maintain a stack of scanned files/directories
    LinkedList<String> fsEntryStack = new LinkedList<String>();
    fsEntryStack.addFirst(path);

    // While there are directories to process
    while (fsEntryStack.size() > 0)
    {
      String fsEntryName = fsEntryStack.removeFirst();
      try
      {
        File currDir = new File(fsEntryName);
        if (currDir.exists() && currDir.isDirectory())
        {
          // files to process
          if (includeFiles)
          {
            // get files
            File[] fis = filterIn(currDir.listFiles(), new HashSet<FileAttribute>(Arrays.asList(new FileAttribute[] {FileAttribute.File})));
            fis = hideScanned(fis, hiddenMode, failMode, collectedScanErrors);

            List<String> dirFiles = new ArrayList<String>(64);
            for (File fi : fis)
              dirFiles.add(fi.getAbsolutePath());

            // sort
            dirFiles = sortScanned(dirFiles, sortMode);

            // next files to process
            for (int i = dirFiles.size() - 1; i >= 0; i--)
              fsEntryStack.addFirst(dirFiles.get(i));
          }

          // check if we need to include directories in results
          if (includeDirectories)
            result.addAll(Linq.toList(Linq.select(hideScanned(new File[] {currDir}, hiddenMode, failMode, collectedScanErrors),
                getAbsolutePath())));

          File[] dis = filterIn(currDir.listFiles(),
              new HashSet<FileAttribute>(Arrays.asList(new FileAttribute[] {FileAttribute.Directory})));
          dis = hideScanned(dis, hiddenMode, failMode, collectedScanErrors);

          List<String> dirDirs = new ArrayList<String>(64);
          for (File dirDir : dis)
            dirDirs.add(dirDir.getAbsolutePath());

          // sort
          dirDirs = sortScanned(dirDirs, sortMode);

          // next directories to process
          for (int i = dirDirs.size() - 1; i >= 0; i--)
            fsEntryStack.addFirst(dirDirs.get(i));
        } else if (currDir.exists() && currDir.isFile())
        {
          File currFile = currDir;

          // check if we need to include files in results
          if (includeFiles)
            result.addAll(Linq.toList(Linq.select(hideScanned(new File[] {currFile}, hiddenMode, failMode, collectedScanErrors),
                getAbsolutePath())));
        }
      }
      catch(Exception e)
      {
View Full Code Here

   * @throws NullPointerException An argument is null
   */
  @Validate
  public void addAction(@NotNull final Predicate1<T> predicate, @NotNull final Action1<T> action)
  {
    val func = actionToFunction(action);
    cases.add(new Pair<Predicate1<T>, Function1<T, TResult>>(predicate, func));
  }
View Full Code Here

TOP

Related Classes of lombok.val

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.