2009-04-10 7 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

+2

Apacheのcommons-langから#StringUtils#joinを使用できませんでしたか? –

+0

これは可能な "homegrow"ユーティリティクラスの唯一の例です – dfa

答えて

4

ロギングと電子メール機能を持​​つユーティリティクラスでの作業以来、約2ダースのプロジェクトで単純なファイルを再利用しています。拡張メソッドを含む拡張クラス。など基本的にレポートサービスのWebサービスを利用し、エクセル、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

ロガークラス:ログファイル内の制御の流れを記録します。

11

System.Object - ほとんどすべてのタイプがそれを拡張します。

1

設定リーダー/セッター:INI/xmlファイルから設定を読み取り、アプリケーション

1

の環境を設定し、ほとんどの再利用しますか?うーん...

ブースト:: shared_ptrの<>再利用<>

おそらく最も

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(...)に変更する必要があります –

+0

またはGuava Throwablesを使用してください – takacsot

1

A ConcurrentDictionary私が書いた、私は今、どこにでも使用するように見える

+0

そしてそれはどこですか? – nawfal

+1

これはここにあります:http://concurrent.codeplex.com/私は非常に*長い前にそれを書きましたが、私はそれがどれほど良いか分かりません。 – Martin

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; 
} 
+5

これはバグです。 http://www.dataip.co.uk/Reference/LeapYear.phpあなたのアルゴリズムは、1900年には29日に28を返します。 なぜジョーダの時間を使用しないのですか? – I82Much

関連する問題