首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

gamecenter有用代码(1)

2012-06-28 
gamecenter有用代码(一)1、接口XCacheFactorypublic interface XCacheFactory {/*** 根据指定prefix来获取X

gamecenter有用代码(一)
1、接口XCacheFactory

public interface XCacheFactory {    /**     * 根据指定prefix来获取XCache实例     *      * @param <T>     * @param prefix      * @param klass value的类型     * @param isCounter 是否用作counter     * @return     */    public <T> XCache<T> getCache(String prefix, Class<T> klass, boolean isCounter);}


2、XCacheFactoryImpl
public class XCacheFactoryImpl implements XCacheFactory {    private MemcachedClientFactory memcachedClientFactory = new MemcachedClientFactory();    public MemCachedClient getMemcachedClient(String namespace) {        return memcachedClientFactory.getClientByNamespace(namespace);    }@Overridepublic <T> XCache<T> getCache(String prefix, Class<T> valueClass, boolean isCounter) {return new XCacheImpl<T>(prefix, this, valueClass, isCounter);}}


3、负责{@link MemCachedClient}的创建
public MemcachedClientFactory() {        init();    }    /**     * 初始化     */    public void init() {        namespaceFactory = ZooKeeperBasedNamespaceFactory.getInstance();        poolFactory = ZooKeeperBasedPoolFactory.getInstance();        poolFactory.addCallback(poolConfigModifiedCallback); //注册回调    }


4、获取时间差
long cur3 = System.currentTimeMillis();if (logger.isDebugEnabled()) {                logger.debug("build apppage:" + (System.currentTimeMillis() - cur3));            }

5、Iterator迭代器的使用
// 匹配appPageSupport                if (CollectionUtils.isNotEmpty(appPageSupportList)) {                    for (Iterator<AppPageSupport> iterator = appPageSupportList.iterator(); iterator                            .hasNext();) {                        AppPageSupport appPageSupportTemp = (AppPageSupport) iterator.next();                        if (appPageSupportTemp.getAppPageId() == appPageEntity.getId()) {                            appPageSupports.add(appPageSupportTemp);                            iterator.remove();                        }                    }                }


6、ResourceBundle

使用ResourceBundle访问本地资源
        在设计时,我们往往需要访问一些适合本地修改的配置信息,如果作为静态变量,那么每次修改都需要重新编译一个class,.config保存此类信息并不适合,这时我们需要ResourceBundle。
   通过ResourceBundle,我们需要访问位于/WEB-INF/classes目录下的一个后缀名为properties的文本类型文件,从里面读取我们需要的值。

    Locale locale = Locale.getDefault();
    ResourceBundle localResource = ResourceBundle.getBundle("ConnResource", locale);

    String value = localResource.getString("test");
    System.out.println("ResourceBundle: " + value);

    这里对应了/WEB-INF/class/ConnResource.properties文件内容为:

    test=hello world

    打印出来的结果就是hello world


private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("cmsconf");

cmsconf.properties的内容如下:
hot0=[{name:'\u7f8e\u56fe\u79c0\u79c0',url:'/page/22?', photo:'fmn054/original/20110812/1550/large_Qb6h_2890000000875c92.jpg', displayOrder:1},{name:'\u62c9\u9614',url:'/page/16?', photo:'fmn050/original/20110812/1555/large_HNOE_2890000000885c92.jpg', displayOrder:2}]


7、JSON字符串的处理
List<Brand> brands = cache.get(TOP_PAGE_KEY + os, List.class);String hotStr = RESOURCE_BUNDLE.getString("hot" + os);if (hotStr != null) {    JSONArray jarr = JSONArray.fromObject(hotStr);    brands = (List<Brand>) JSONArray.toCollection(jarr, Brand.class);            }


8、排序比较
if (CollectionUtils.isNotEmpty(brands)) {     Collections.sort(brands, new Comparator<Brand>() {     public int compare(Brand o1, Brand o2) {         return o1.getOrder() - o2.getOrder();     };     });     cache.save(TOP_PAGE_KEY + os, brands, List.class, pageDefaultCacheTime);            }


sort(appPageList, new ComparatorAppPage() {            @Override            public int compare(AppPage page0, AppPage page1) {                int compareOrder = 0;                //如展示顺序相同则比较更新时间                if (page0.getAddTime() != null && page1.getAddTime() != null) {                    compareOrder = page0.getAddTime().compareTo(page1.getAddTime());                    if (compareOrder == 0) {                        return page0.getDisplayOrder() - page1.getDisplayOrder();                    } else {                        return compareOrder;                    }                } else {                    return 0;                }            }        });


9、afterPropertiesSet初始化
 public void afterPropertiesSet() throws Exception {        Assert.notNull(appPageRelationDAO, "appPageRelationDAO is required!");        Assert.notNull(appPageDAO, "appPageDAO is required!");        Assert.notNull(appPageSupportDAO, "appPageSupportDAO is required!");        Assert.notNull(friendsFacade, "friendsFacade is required!");    }


10、对修改后的数据进行重新加载(缓存)
public int updateAppPageSupport(AppPageSupport appPageSupport) {        int result = appPageSupportDAO.update(appPageSupport);        reloadApp(appPageSupport.getAppPageId());        return result;    }


11、List to String
private String[] convertToStringArray(List<Integer> keys) {        if (CollectionUtils.isEmpty(keys)) {            return null;        }        int listSize = keys.size();        String[] array = new String[listSize];        int emptyKey = 0;        for (int i = 0; i < listSize; i++) {            Integer key = keys.get(i);            if (key != null) {                array[i] = key.toString();            } else {                emptyKey++;            }        }        if (emptyKey == listSize) {            return null;        }        return array;    }


12、List to Integer
private List<Integer> convertToIntegerList(Integer[] keys) {        if (ArrayUtils.isEmpty(keys)) {            return null;        }        List<Integer> list = new ArrayList<Integer>();        for (Integer key : keys) {            list.add(key);        }        return list;    }

热点排行