Language agnostic 什么';你最喜欢的课程是什么?
每个程序员在一段时间后都会得到一组实用程序类。其中一些是真正的编程珍珠,它们在您的几个项目中被重用。例如,在java中:Language agnostic 什么';你最喜欢的课程是什么?,language-agnostic,oop,Language Agnostic,Oop,每个程序员在一段时间后都会得到一组实用程序类。其中一些是真正的编程珍珠,它们在您的几个项目中被重用。例如,在java中: class Separator { private String separator; private boolean called; public Separator(String aSeparator) { separator = aSeparator; called =
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;
}
}
}
以及:
公共类JoinHelper{
公共静态字符串联接(T…元素){
返回joinArray(“,元素);
}
公共静态字符串联接(字符串分隔符、T…元素){
返回joinArray(分隔符、元素);
}
私有静态字符串joinArray(字符串sep,T[]元素){
StringBuilder StringBuilder=新的StringBuilder();
分离器=新分离器(sep);
对于(T元素:元素){
stringBuilder.append(分隔符).append(元素);
}
返回stringBuilder.toString();
}
}
你的最容易重用的类是什么 Logger类:它将控制流记录在日志文件中。-几乎我所有的类型都扩展了它。配置读取器/设置器:它从ini/xml文件读取配置,并设置应用程序的环境。重用最多?嗯 boost::与boost::弱\u ptr共享\u ptr 可能是重复使用最多的(也可能是最高的性价比)全球 只是一个带有静态DBConnString的简单类,以及一些其他应用程序范围的设置
自从使用.Net(一个具有日志记录和电子邮件功能的实用程序类)以来,我们已经在大约20多个项目中重用了这个简单文件。包含扩展方法的扩展类。一个reporting类,它基本上利用ReportingServicesWeb服务,使报表以excel、pdf等格式流式传输变得容易 示例…
1.)实用程序类(静态) 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;
}
公共静态IEnumerable WhereIf(此IEnumerable源、bool条件、Func谓词)
{
如果(条件)
返回source.Where(谓词);
其他的
返回源;
}
重复使用最多但最无聊的:
public static void handleException(Exception e) throws RuntimeException {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RuntimeException(e); //NOPMD
}
减少无聊(也包括构建列表和集合的方法):
/**
*构建基于Bean列表的映射。
*
*@param items Bean列表项
*@param keyField Bean字段,将作为映射元素的键(非空)
*@返回一个基于Bean列表的映射
*/
@抑制警告(“未选中”)
公共静态地图buildMapFromCollection(最终集合项,
布尔链接映射,
最后一个字符串键域,
最终类(键类型){
if(items==null){
return Collections.emptyMap();
}
if(keyField==null){
抛出新的IllegalArgumentException(“KeyField为null”);
}
最终地图结果;
if(linkedMap){
结果=新建LinkedHashMap();
}否则{
结果=新的HashMap();
}
BeanMapper映射器=null;
用于(最终T项目:项目){
if(映射器==null){
映射器=新BeanMapper(item.getClass());
}
最终K键=(K)mapper.getFieldValue(项,键域);
结果。放置(键、项);
}
返回结果;
}
我写的一本ConcurrentDictionary,现在似乎到处都在使用(我写了很多并行程序)请创建这个社区wiki。否则,您就有被关闭的风险。难道您不能使用Apache的commons lang中的StringUtils#join吗?这只是可能的“homegrow”实用程序类的一个例子太糟糕了,这是一个错误。您的算法将返回1900的29,而它应该是28。为什么不使用乔达时间或类似的东西呢?你应该把它改名为doNotHandleException(…):)或者只使用番石榴扔在这里:我很久以前就写过,但我不知道它有多好。
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;
}
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;
}
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;
}