Esempio n. 1
0
  public Object read(Buffer buf) {
    HeaderInfo info = parseHeader(buf);
    Object obj = null;

    switch (info.type) {
      case TYPE_INTEGER:
        obj = New.i8((byte) info.value);
        break;
      case TYPE_I8:
        obj = New.i8(buf.readI8());
        break;
      case TYPE_I16:
        obj = New.i16(buf.readI16());
        break;
      case TYPE_I32:
        obj = New.i32(buf.readI32());
        break;
      case TYPE_I64:
        obj = New.i64(buf.readI64());
        break;
      case TYPE_F32:
        obj = New.f32(buf.readF32());
        break;
      case TYPE_F64:
        obj = New.f64(buf.readF64());
        break;
      case TYPE_STRING:
        obj = readString(buf, info.length);
        break;
      case TYPE_TRUE:
        obj = New.bool(true);
        break;
      case TYPE_FALSE:
        obj = New.bool(false);
        break;
      case TYPE_NULL:
        obj = null;
        break;
      case TYPE_LIST:
        obj = readList(buf, info.length);
        break;
      case TYPE_MAP:
        obj = readMap(buf, info.length);
        break;
      default:
        if (W) Log.warn("read variant: unknown type, returning null.");
        obj = null;
    }

    New.release(info);
    return obj;
  }
Esempio n. 2
0
 /**
  * Split a string into an array of strings using the given separator. A null string will result in
  * a null array, and an empty string in a zero element array.
  *
  * @param s the string to split
  * @param separatorChar the separator character
  * @param trim whether each element should be trimmed
  * @return the array list
  */
 public static String[] arraySplit(String s, char separatorChar, boolean trim) {
   if (s == null) {
     return null;
   }
   int length = s.length();
   if (length == 0) {
     return new String[0];
   }
   ArrayList<String> list = New.arrayList();
   StringBuilder buff = new StringBuilder(length);
   for (int i = 0; i < length; i++) {
     char c = s.charAt(i);
     if (c == separatorChar) {
       String e = buff.toString();
       list.add(trim ? e.trim() : e);
       buff.setLength(0);
     } else if (c == '\\' && i < length - 1) {
       buff.append(s.charAt(++i));
     } else {
       buff.append(c);
     }
   }
   String e = buff.toString();
   list.add(trim ? e.trim() : e);
   String[] array = new String[list.size()];
   list.toArray(array);
   return array;
 }
Esempio n. 3
0
 public void delete() {
   try {
     Files.delete(file);
   } catch (IOException e) {
     throw New.unchecked(e);
   }
 }
Esempio n. 4
0
 public OutputStream outputStream() {
   try {
     return Files.newOutputStream(file);
   } catch (IOException e) {
     throw New.unchecked(e);
   }
 }
public abstract class Visitor<T, R> {
  private boolean isDone;
  private ConcurrentLinkedQueue<R> founds = New.concurrentLinkedQueue();

  public abstract void visit(T value);

  public boolean isDone() {
    return isDone;
  }

  public void done() {
    this.isDone = true;
  }

  public void add(R found) {
    this.founds.add(found);
  }

  public R getFirst() {
    if (founds.isEmpty()) return null;
    return founds.peek();
  }

  public ConcurrentLinkedQueue<R> getFounds() {
    return founds;
  }
}
Esempio n. 6
0
 public FileResource create() {
   try {
     Files.createFile(file);
   } catch (IOException e) {
     throw New.unchecked(e);
   }
   return this;
 }
Esempio n. 7
0
public class Interfaces {

  /** Represents the default interface modifier. */
  public static final InterfaceModifiers DEFAULT_MODIFIER = New.interfaceModifiers();

  /** Represents "public" interface modifier. */
  public static final InterfaceModifiers PUBLIC =
      New.interfaceModifiers(InterfaceModifierValue.PUBLIC);

  /** Represents "public abstract" interface modifiers. */
  public static final InterfaceModifiers PUBLIC_ABSTRACT =
      New.interfaceModifiers(InterfaceModifierValue.PUBLIC, InterfaceModifierValue.ABSTRACT);

  /** Represents "abstract" interface modifier. */
  public static final InterfaceModifiers ABSTRACT =
      New.interfaceModifiers(InterfaceModifierValue.ABSTRACT);
}
 /**
  * Get the list of keys.
  *
  * @return all keys
  */
 public ArrayList<Value> keys() {
   ArrayList<Value> list = New.arrayList(size);
   for (Value k : keys) {
     if (k != null && k != ValueNull.DELETED) {
       list.add(k);
     }
   }
   return list;
 }
 /**
  * Get the list of values.
  *
  * @return all values
  */
 public ArrayList<V> values() {
   ArrayList<V> list = New.arrayList(size);
   int len = keys.length;
   for (int i = 0; i < len; i++) {
     Value k = keys[i];
     if (k != null && k != ValueNull.DELETED) {
       list.add(values[i]);
     }
   }
   return list;
 }
Esempio n. 10
0
 public String getName(int value) {
   switch (value) {
     case 0:
       return New.name();
     case 1:
       return Online.name();
     case 2:
       return Populer.name();
     case 3:
       return Group.name();
     case 4:
       return Follow.name();
     default:
       return Followed.name();
   }
 }
Esempio n. 11
0
  public void scan(Buffer buf) {
    HeaderInfo info = parseHeader(buf);
    int len = info.length;

    switch (info.type) {
      case TYPE_LIST:
        while (len-- > 0) {
          scan(buf);
        }
        break;
      case TYPE_MAP:
        while (len-- > 0) {
          scan(buf); // scan key
          scan(buf); // scan value
        }
        break;
      default:
        buf.drop(info.length);
        break;
    }

    New.release(info);
  }
Esempio n. 12
0
  public static void main(String args[]) {
    System.out.println("Hello GoogleStudyJam");

    New ob = new New();
    ob.Print();
  }
Esempio n. 13
0
 /** Delete all registered temp files. */
 public void deleteAll() {
   for (String tempFile : New.arrayList(refMap.values())) {
     deleteFile(null, tempFile);
   }
   deleteUnused();
 }
Esempio n. 14
0
/** This class deletes temporary files when they are not used any longer. */
public class TempFileDeleter {

  private final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
  private final HashMap<PhantomReference<?>, String> refMap = New.hashMap();

  private TempFileDeleter() {
    // utility class
  }

  public static TempFileDeleter getInstance() {
    return new TempFileDeleter();
  }

  /**
   * Add a file to the list of temp files to delete. The file is deleted once the file object is
   * garbage collected.
   *
   * @param fileName the file name
   * @param file the object to monitor
   * @return the reference that can be used to stop deleting the file
   */
  public synchronized Reference<?> addFile(String fileName, Object file) {
    IOUtils.trace("TempFileDeleter.addFile", fileName, file);
    PhantomReference<?> ref = new PhantomReference<Object>(file, queue);
    refMap.put(ref, fileName);
    deleteUnused();
    return ref;
  }

  /**
   * Delete the given file now. This will remove the reference from the list.
   *
   * @param ref the reference as returned by addFile
   * @param fileName the file name
   */
  public synchronized void deleteFile(Reference<?> ref, String fileName) {
    if (ref != null) {
      String f2 = refMap.remove(ref);
      if (f2 != null) {
        if (SysProperties.CHECK) {
          if (fileName != null && !f2.equals(fileName)) {
            DbException.throwInternalError("f2:" + f2 + " f:" + fileName);
          }
        }
        fileName = f2;
      }
    }
    if (fileName != null && FileUtils.exists(fileName)) {
      try {
        IOUtils.trace("TempFileDeleter.deleteFile", fileName, null);
        FileUtils.tryDelete(fileName);
      } catch (Exception e) {
        // TODO log such errors?
      }
    }
  }

  /** Delete all registered temp files. */
  public void deleteAll() {
    for (String tempFile : New.arrayList(refMap.values())) {
      deleteFile(null, tempFile);
    }
    deleteUnused();
  }

  /** Delete all unused files now. */
  public void deleteUnused() {
    while (queue != null) {
      Reference<? extends Object> ref = queue.poll();
      if (ref == null) {
        break;
      }
      deleteFile(ref, null);
    }
  }

  /**
   * This method is called if a file should no longer be deleted if the object is garbage collected.
   *
   * @param ref the reference as returned by addFile
   * @param fileName the file name
   */
  public void stopAutoDelete(Reference<?> ref, String fileName) {
    IOUtils.trace("TempFileDeleter.stopAutoDelete", fileName, ref);
    if (ref != null) {
      String f2 = refMap.remove(ref);
      if (SysProperties.CHECK) {
        if (f2 == null || !f2.equals(fileName)) {
          DbException.throwInternalError(
              "f2:" + f2 + " " + (f2 == null ? "" : f2) + " f:" + fileName);
        }
      }
    }
    deleteUnused();
  }
}
Esempio n. 15
0
 public Object read(Buffer buf) {
   return New.i16(buf.readI16());
 }
Esempio n. 16
0
/** A simple hash table with an optimization for the last recently used object. */
public class SmallMap {
  private HashMap<Integer, Object> map = New.hashMap();
  private Object cache;
  private int cacheId;
  private int lastId;
  private int maxElements;

  /**
   * Create a map with the given maximum number of entries.
   *
   * @param maxElements the maximum number of entries
   */
  public SmallMap(int maxElements) {
    this.maxElements = maxElements;
  }

  /**
   * Add an object to the map. If the size of the map is larger than twice the maximum size, objects
   * with a low id are removed.
   *
   * @param id the object id
   * @param o the object
   * @return the id
   */
  public int addObject(int id, Object o) {
    if (map.size() > maxElements * 2) {
      Iterator<Integer> it = map.keySet().iterator();
      while (it.hasNext()) {
        Integer k = it.next();
        if (k.intValue() + maxElements < lastId) {
          it.remove();
        }
      }
    }
    if (id > lastId) {
      lastId = id;
    }
    map.put(id, o);
    cacheId = id;
    cache = o;
    return id;
  }

  /**
   * Remove an object from the map.
   *
   * @param id the id of the object to remove
   */
  public void freeObject(int id) {
    if (cacheId == id) {
      cacheId = -1;
      cache = null;
    }
    map.remove(id);
  }

  /**
   * Get an object from the map if it is stored.
   *
   * @param id the id of the object
   * @param ifAvailable only return it if available, otherwise return null
   * @return the object or null
   * @throws SQLException if isAvailable is false and the object has not been found
   */
  public Object getObject(int id, boolean ifAvailable) throws SQLException {
    if (id == cacheId) {
      return cache;
    }
    Object obj = map.get(id);
    if (obj == null && !ifAvailable) {
      throw Message.getSQLException(ErrorCode.OBJECT_CLOSED);
    }
    return obj;
  }
}
Esempio n. 17
0
  // return type, length
  private HeaderInfo parseHeader(Buffer buf) throws RemoteException {
    byte value = buf.readI8();
    int h = value & 0xff;
    int h1 = h & ~MASK_LENGTH_1;
    int h2 = h & ~MASK_LENGTH_2;
    int v1 = h & MASK_LENGTH_1;
    int v2 = h & MASK_LENGTH_2;

    HeaderInfo info = (HeaderInfo) New.get(HeaderInfo.class);

    if (value >= -32) {
      info.type = TYPE_INTEGER;
      info.value = value;
      info.length = 0;
      return info;
    }

    switch (h1) {
      case TYPE_STRING:
      case TYPE_LIST:
        info.type = h1;
        if (v1 < MASK_LENGTH_1) {
          info.length = v1;
        } else {
          info.length = buf.readI32();
        }
        return info;
    }

    switch (h2) {
      case TYPE_MAP:
        info.type = h2;
        if (v2 < MASK_LENGTH_2) {
          info.length = v2;
        } else {
          info.length = buf.readI32();
        }
        return info;
    }

    switch (h) {
      case TYPE_NULL:
      case TYPE_FALSE:
      case TYPE_TRUE:
        info.type = h;
        info.length = 0;
        break;
      case TYPE_I8:
        info.type = h;
        info.length = 1;
        break;
      case TYPE_I16:
        info.type = h;
        info.length = 2;
        break;
      case TYPE_I32:
      case TYPE_F32:
        info.type = h;
        info.length = 4;
        break;
      case TYPE_I64:
      case TYPE_F64:
        info.type = h;
        info.length = 8;
        break;
      default:
        if (W) Log.warn("parsed variant: unknown type, parsed to NULL.");
        info.type = TYPE_NULL;
        info.length = 0;
    }

    return info;
  }
Esempio n. 18
0
/** This class deletes temporary files when they are not used any longer. */
public class TempFileDeleter {

  private final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
  private final HashMap<PhantomReference<?>, TempFile> refMap = New.hashMap();

  private TempFileDeleter() {
    // utility class
  }

  public static TempFileDeleter getInstance() {
    return new TempFileDeleter();
  }

  /** Contains information about a file. */
  static class TempFile {

    /** The file name. */
    String fileName;

    /** The last modified date of this file. */
    long lastModified;
  }

  /**
   * Add a file to the list of temp files to delete. The file is deleted once the file object is
   * garbage collected.
   *
   * @param fileName the file name
   * @param file the object to monitor
   * @return the reference that can be used to stop deleting the file
   */
  public synchronized Reference<?> addFile(String fileName, Object file) {
    FileUtils.trace("TempFileDeleter.addFile", fileName, file);
    PhantomReference<?> ref = new PhantomReference<Object>(file, queue);
    TempFile f = new TempFile();
    f.fileName = fileName;
    f.lastModified = FileUtils.getLastModified(fileName);
    refMap.put(ref, f);
    deleteUnused();
    return ref;
  }

  /**
   * Update the last modified date of the auto-delete reference. If the file was modified after
   * that, it will not be deleted (because it might have been deleted and then re-created).
   *
   * @param ref the reference
   */
  public synchronized void updateAutoDelete(Reference<?> ref) {
    TempFile f2 = refMap.get(ref);
    if (f2 != null) {
      String fileName = f2.fileName;
      long mod = FileUtils.getLastModified(fileName);
      f2.lastModified = mod;
    }
  }

  /**
   * Delete the given file now. This will remove the reference from the list.
   *
   * @param ref the reference as returned by addFile
   * @param fileName the file name
   */
  public synchronized void deleteFile(Reference<?> ref, String fileName) {
    if (ref != null) {
      TempFile f2 = refMap.remove(ref);
      if (f2 != null) {
        if (SysProperties.CHECK && fileName != null && !f2.fileName.equals(fileName)) {
          Message.throwInternalError("f2:" + f2.fileName + " f:" + fileName);
        }
        fileName = f2.fileName;
        long mod = FileUtils.getLastModified(fileName);
        if (mod != f2.lastModified) {
          // the file has been deleted and a new one created
          // or it has been modified afterwards
          return;
        }
      }
    }
    if (fileName != null && FileUtils.exists(fileName)) {
      try {
        FileUtils.trace("TempFileDeleter.deleteFile", fileName, null);
        FileUtils.tryDelete(fileName);
      } catch (Exception e) {
        // TODO log such errors?
      }
    }
  }

  /** Delete all registered temp files. */
  public void deleteAll() {
    for (TempFile tempFile : refMap.values()) {
      deleteFile(null, tempFile.fileName);
    }
    deleteUnused();
  }

  /** Delete all unused files now. */
  public void deleteUnused() {
    while (queue != null) {
      Reference<? extends Object> ref = queue.poll();
      if (ref == null) {
        break;
      }
      deleteFile(ref, null);
    }
  }

  /**
   * This method is called if a file should no longer be deleted if the object is garbage collected.
   *
   * @param ref the reference as returned by addFile
   * @param fileName the file name
   */
  public void stopAutoDelete(Reference<?> ref, String fileName) {
    FileUtils.trace("TempFileDeleter.stopAutoDelete", fileName, ref);
    if (ref != null) {
      TempFile f2 = refMap.remove(ref);
      if (SysProperties.CHECK && (f2 == null || !f2.fileName.equals(fileName))) {
        Message.throwInternalError(
            "f2:" + f2 + " " + (f2 == null ? "" : f2.fileName) + " f:" + fileName);
      }
    }
    deleteUnused();
  }
}
Esempio n. 19
0
 public SoftHashMap() {
   map = New.hashMap();
 }