📄 memorysyncsource.java
字号:
/** * Copyright (C) 2003-2004 Funambol * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package sync4j.framework.engine.source;import java.util.Collection;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.security.Principal;import java.sql.Timestamp;import sync4j.framework.engine.SyncProperty;import sync4j.framework.engine.SyncItemKey;import sync4j.framework.engine.SyncItem;import sync4j.framework.engine.SyncItemImpl;import sync4j.framework.engine.SyncItemState;import sync4j.framework.engine.source.SyncSourceException;import sync4j.framework.engine.source.SyncSource;/* * This source represents a <i>SyncSource</i> where all items are kept in memory. * A <i>MemorySyncSource</i> can be created passing in the constructor the name * of the source and the items or creating an anonymous source with the default * constructor and then initialize the source with <i>initialize(...)</i>. * * @author Stefano Fornari @ Funambol * * @version $Id: MemorySyncSource.java,v 1.11 2004/04/13 09:37:32 luigia Exp $ */public class MemorySyncSource extends AbstractSyncSourceimplements SyncSource, java.io.Serializable { // --------------------------------------------------------------- Constants public static final String NAME = "MemorySyncSource"; // ----------------------------------------------------- Private data member // the content of the SyncML client private ArrayList unchangedSyncItems = new ArrayList(); private ArrayList newSyncItems = new ArrayList(); private ArrayList updatedSyncItems = new ArrayList(); private ArrayList deletedSyncItems = new ArrayList(); // ------------------------------------------------------------ Constructors public MemorySyncSource() {} public MemorySyncSource(String name, String type, String sourceURI) { super(name, type, sourceURI); } public MemorySyncSource(String name , List unchangedSyncItems, List deletedSyncItems , List newSyncItems , List updatedSyncItems ) { // // The items are added so no need to clone the lists // super(name); initialize(unchangedSyncItems, deletedSyncItems, newSyncItems, updatedSyncItems); } // ---------------------------------------------------------- Public Methods /** * Sets the name of the source. * * @param name the new name */ public void setName(String name) { this.name = name; } /* * @see SyncSource */ public SyncItem setSyncItem(Principal principal, SyncItem syncItem) throws SyncSourceException { unchangedSyncItems.remove(syncItem); newSyncItems.remove (syncItem); deletedSyncItems.add (syncItem); updatedSyncItems.add(syncItem); SyncItem newSyncItem = new SyncItemImpl(this, syncItem.getKey().getKeyAsString(), SyncItemState.NEW); newSyncItem.setProperties(syncItem.getProperties()); return newSyncItem; } /* * @see SyncSource */ public SyncItem[] setSyncItems(Principal principal, SyncItem[] syncItems) throws SyncSourceException { SyncItem[] ret = new SyncItem[syncItems.length]; for (int i=0; (i<syncItems.length); ++i) { ret[i] = setSyncItem(principal, syncItems[i]); } // next i return ret; } /* * @see SyncSource * * This implementation cycles through all SyncItems looking for the * specified key. */ public SyncItem getSyncItemFromId(Principal principal, SyncItemKey syncItemKey) throws SyncSourceException { SyncItem[] all = this.getAllSyncItems(principal); for (int i=0; ((all != null) && (i<all.length)); ++i) { if (syncItemKey.equals(all[i].getKey())) { return all[i]; } } return null; } /* * @see SyncSource * */ public SyncItem[] getSyncItemsFromIds(Principal principal, SyncItemKey[] syncItemKeys) throws SyncSourceException { ArrayList ret = new ArrayList(); SyncItem syncItem = null; for (int i=0; ((syncItemKeys != null) && (i<syncItemKeys.length)); ++i) { syncItem = getSyncItemFromId(principal, syncItemKeys[i]); if (syncItem != null) { ret.add(syncItem); } } return (SyncItem[])ret.toArray(new SyncItem[ret.size()]); } /* * @see SyncSource */ public SyncItem[] getUnchangedSyncItems(Principal principal, Timestamp since) throws SyncSourceException { return (SyncItem[])unchangedSyncItems.toArray(new SyncItem[0]); } /* * @see SyncSource */ public SyncItemKey[] getUnchangedSyncItemKeys(Principal principal, Timestamp since) throws SyncSourceException { return extractKeys(unchangedSyncItems); } /* * @see SyncSource */ public SyncItemKey[] getNewSyncItemKeys(Principal principal, Timestamp since ) throws SyncSourceException { return extractKeys(newSyncItems); } /* * @see SyncSource */ public SyncItem[] getNewSyncItems(Principal principal, Timestamp since ) throws SyncSourceException { return (SyncItem[])newSyncItems.toArray(new SyncItem[0]); } /* * @see SyncSource */ public SyncItemKey[] getDeletedSyncItemKeys(Principal principal, Timestamp since ) throws SyncSourceException { return extractKeys(deletedSyncItems); } /* * @see SyncSource */ public SyncItem[] getDeletedSyncItems(Principal principal, Timestamp since ) throws SyncSourceException { return (SyncItem[])deletedSyncItems.toArray(new SyncItem[0]); } /** * @return an array of keys containing the <i>SyncItem</i>'s key of the updated * items after the last synchronizazion. */ public SyncItemKey[] getUpdatedSyncItemKeys(Principal principal, Timestamp since ) throws SyncSourceException { return extractKeys(updatedSyncItems); } /** * @see SyncSource */ public SyncItem[] getUpdatedSyncItems(Principal principal, Timestamp since ) throws SyncSourceException { return (SyncItem[])updatedSyncItems.toArray(new SyncItem[0]); } /* * @see SyncSource */ public void removeSyncItem(Principal principal, SyncItem syncItem) throws SyncSourceException { unchangedSyncItems.remove(syncItem); newSyncItems.remove (syncItem); updatedSyncItems.remove (syncItem); deletedSyncItems.add(syncItem); } /* * @see SyncSource */ public void removeSyncItems(Principal principal, SyncItem[] syncItems) throws SyncSourceException { for (int i=0; i<syncItems.length; ++i) { removeSyncItem(principal, syncItems[i]); } // next i } /** * @see SyncSource */ public SyncItem[] getAllSyncItems(Principal principal) throws SyncSourceException { SyncItem[] unchangedItems = getUnchangedSyncItems(principal, null); SyncItem[] newItems = getNewSyncItems(principal, null); SyncItem[] deletedItems = getDeletedSyncItems(principal, null); SyncItem[] updatedItems = getUpdatedSyncItems(principal, null); SyncItem[] allItems = new SyncItem[ unchangedItems.length + newItems.length + deletedItems.length + updatedItems.length ]; int c = 0; for(int i=0; (i<unchangedItems.length); ++i) { allItems[c++] = unchangedItems[i]; } for(int i=0; (i<newItems.length); ++i) { allItems[c++] = newItems[i]; } for(int i=0; (i<deletedItems.length); ++i) { allItems[c++] = deletedItems[i]; } for(int i=0; (i<updatedItems.length); ++i) { allItems[c++] = updatedItems[i]; } return allItems; } /** * @see SyncSource */ public boolean isModified(Principal principal, Timestamp since) { try { return (getDeletedSyncItems(principal, since).length > 0) || (getNewSyncItems(principal, since).length > 0) || (getUpdatedSyncItems(principal, since).length > 0); } catch (Exception e) { e.printStackTrace(); return false; } } /** * Initializes this MemorySyncSource with items. * * @param existingItems the existing items * @param deletedItems the deleted items * @param newItems the created items * @param updatedItems the updated items */ public void initialize(List existingItems, List deletedItems , List newItems , List updatedItems ) { unchangedSyncItems.clear(); deletedSyncItems.clear() ; newSyncItems.clear() ; updatedSyncItems.clear() ; unchangedSyncItems.addAll(existingItems); deletedSyncItems.addAll(deletedItems) ; newSyncItems.addAll(newItems) ; updatedSyncItems.addAll(updatedItems ) ; // // We have to make sure that the complete view is consistent so that // new, modified or deleted items are not in the unchanged list; that // deleted items are not in new, update and unchanged lists, and so on. // SyncItem syncItem = null; Iterator i = deletedSyncItems.iterator(); while (i.hasNext()) { syncItem = (SyncItem)i.next(); unchangedSyncItems.remove(syncItem); newSyncItems.remove(syncItem) ; updatedSyncItems.remove(syncItem) ; } i = updatedSyncItems.iterator(); while (i.hasNext()) { syncItem = (SyncItem)i.next(); unchangedSyncItems.remove(syncItem); newSyncItems.remove(syncItem) ; } i = newSyncItems.iterator(); while (i.hasNext()) { syncItem = (SyncItem)i.next(); unchangedSyncItems.remove(syncItem); } } /** * Search the items for an item with exactly the same binary content. * * @return an item from a twin item. Each source implementation is free to * interpret this as it likes (i.e.: comparing all fields). * * @param principal * @param syncItemTwin the twin item * * @throws SyncSourceException in case of errors (for instance if the * underlying data store runs into problems) * */ public SyncItem getSyncItemFromTwin(Principal principal, SyncItem syncItemTwin) throws SyncSourceException { SyncItem[] syncItems = getSyncItemsFromTwins(principal, new SyncItem[] {syncItemTwin}); if ((syncItems == null) || (syncItems.length == 0)) { return null; // not found } return syncItems[0]; } /** * Two items are considered twin if their binary content, converted to * String are equal. * * @param principal * @param syncItemTwins the twin items * * @return an array of items from corresponding twin items. * * @throws SyncSourceException in case of errors (for instance if the * underlying data store runs into problems) * */ public SyncItem[] getSyncItemsFromTwins(Principal principal, SyncItem[] syncItemTwins) throws SyncSourceException { ArrayList ret = new ArrayList(); String[] contents = new String[syncItemTwins.length]; for (int i=0; i<syncItemTwins.length; ++i) { contents[i] = new String((byte[])syncItemTwins[i].getProperty(SyncItem.PROPERTY_BINARY_CONTENT).getValue()); } SyncItem[] all = getAllSyncItems(principal); String content = null; SyncProperty prop = null; for (int i=0; i<all.length; ++i) { prop = all[i].getProperty(SyncItem.PROPERTY_BINARY_CONTENT); if (prop != null) { content = new String((byte[])prop.getValue()); } for (int j=0; ((content != null) && (j<contents.length)); ++j) { if (content.equals(contents[j])) { ret.add(all[i]); break; } } } return (SyncItem[])ret.toArray(new SyncItem[ret.size()]); } // --------------------------------------------------------- Private methods private SyncItemKey[] extractKeys(Collection syncItems) { SyncItemKey[] keys = new SyncItemKey[syncItems.size()]; SyncItem syncItem = null; int j = 0; for(Iterator i = syncItems.iterator(); i.hasNext(); ++j) { syncItem = (SyncItem)i.next(); keys[j] = syncItem.getKey(); } // next i, j return keys; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -