2009-04-10 49 views
17

过了一段时间,每个程序员都会得到一组实用程序类。其中一些是真正的编程珍珠,它们在你的几个项目中被重复使用。例如,在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; 
      } 
     } 
    } 

和:

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(); 
    } 
} 

什么是最重用的类?

+2

制作这个社区wiki,请。否则,你有可能关闭。 – Randolpho 2009-04-10 16:02:07

+2

难道你不能只使用Apache的commons-lang中的StringUtils#join吗? – 2009-06-04 09:25:11

+0

这只是一个可能的“homegrow”工具​​类的例子 – dfa 2009-06-04 14:07:42

回答

4

有记录和电子邮件功能的实用程序类的工作人员有重复使用简单的文件中大约2十几个项目。包含扩展方法的扩展类。报告类,基本上利用报告服务Web服务,并可以很容易地流报告为Excel,PDF等

例子...
1)工具类(静态)

public static void LogError(Exception ex) 
    { 
     EventLog log = new EventLog(); 
     if (ex != null) 
     { 
      log.Source = ConfigurationManager.AppSettings["EventLog"].ToString(); 
      StringBuilder sErrorMessage = new StringBuilder(); 
      if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null) 
      { 
       sErrorMessage.Append(HttpContext.Current.Request.Url.ToString() + System.Environment.NewLine); 
      } 
      sErrorMessage.Append(ex.ToString()); 
      log.WriteEntry(sErrorMessage.ToString(), EventLogEntryType.Error); 
     } 
    } 

2)扩展类

public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate) 
    { 
     if (condition) 
      return source.Where(predicate); 
     else 
      return source; 
    } 
1

记录器类:它将日志文件中的控制流记录下来。

1

配置读卡器/二传手:从INI/XML文件中读取配置和设置应用程序

1

的环境中重复使用次数最多?嗯...

的boost :: shared_ptr的<>用的boost :: weak_ptr的<>

可能是最重用(也可能是最邦换降压比)

1

全局

只是一个带有静态DBConnString的简单类,以及一些其他应用程序范围的设置。

因为与.net

3

大多数重用,但无聊:

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

    throw new RuntimeException(e); //NOPMD 
} 

不那么无聊(也方法的楼宇名单,并集):

/** 
    * 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; 
    } 
+1

你应该重新命名它doNotHandleException(...):) – 2011-01-25 16:55:27

1

一个ConcurrentDictionary我写的,我现在似乎到处使用(我写的很多并行程序)

3
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; 
} 
相关问题