What's your most reused class?

2019-03-23 18:40发布

Every programmer ends up with a set of utility classes after a while. Some of them are true programming pearls and they are reused in several of your projects. For example, in java:

 class Separator {

        private String separator;
        private boolean called;

        public Separator(String aSeparator) {
            separator = aSeparator;
            called = false;
        }

        @Override
        public String toString() {
            if (!called) {
                called = true;
                return "";
            } else {
                return separator;
            }
        }
    }

and:

public class JoinHelper {

    public static <T> String join(T... elements) {
        return joinArray(" ", elements);
    }

    public static <T> String join(String separator, T... elements) {
        return joinArray(separator, elements);
    }

    private static <T> String joinArray(String sep, T[] elements) {
        StringBuilder stringBuilder = new StringBuilder();
        Separator separator = new Separator(sep);

        for (T element : elements) {
           stringBuilder.append(separator).append(element);
        }

        return stringBuilder.toString();
    }
}

What is your most reused class?

9条回答
贼婆χ
2楼-- · 2019-03-23 19:16

Logger class: Which logs the flow of control in a log file.

查看更多
欢心
3楼-- · 2019-03-23 19:27

Most reused but boring:

public static void handleException(Exception e) throws RuntimeException {
    if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
    }

    throw new RuntimeException(e); //NOPMD
}

Less boring (also methods for building lists and sets):

/**
   * Builds a Map that is based on the Bean List.
   * 
   * @param items Bean List items
   * @param keyField Bean Field that will be key of Map elements (not null)
   * @return a Map that is based on the Bean List
   */
  @SuppressWarnings("unchecked")
  public static <T, K> Map<K, T> buildMapFromCollection(final Collection<T> items,
                                                        boolean linkedMap,
                                                        final String keyField,
                                                        final Class<K> keyType) {
    if (items == null) {
      return Collections.emptyMap();
    }

    if (keyField == null) {
      throw new IllegalArgumentException("KeyField is null");
    }

    final Map<K, T> result;

    if (linkedMap) {
      result = new LinkedHashMap<K, T>();
    } else {
      result = new HashMap<K, T>();
    }

    BeanMapper mapper = null;
    for (final T item : items) {
      if (mapper == null) {
        mapper = new BeanMapper(item.getClass());
      }
      final K key = (K) mapper.getFieldValue(item, keyField);
      result.put(key, item);
    }
    return result;
  }
查看更多
来,给爷笑一个
4楼-- · 2019-03-23 19:32
public static short getLastDayOfMonth(short givenMonth, short givenYear)
{
    short lastDay = 31;
    switch (givenMonth)
    {
        case 4:
        case 6:
        case 9:
        case 11:
            lastDay = 30;
            break;
        case 2:
            if ((int)givenYear % 4 == 0)
            {
                lastDay = 29;
            }
            else
            {
                lastDay = 28;
            }
            break;    
    }
    return lastDay;
}
查看更多
登录 后发表回答