Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tokka.server.communication.data;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. /**
  23. * {@link DataSource} wrapper for {@link Collection}s.
  24. *
  25. * @param <T>
  26. * data type
  27. */
  28. public class ListDataSource<T> extends AbstractDataSource<T> {
  29. private List<T> backend;
  30. /**
  31. * Constructs a new ListDataSource. This method makes a protective copy of
  32. * the contents of the Collection.
  33. *
  34. * @param collection
  35. * initial data
  36. */
  37. public ListDataSource(Collection<T> collection) {
  38. backend = new ArrayList<T>(collection);
  39. }
  40. @Override
  41. public void save(T data) {
  42. if (!backend.contains(data)) {
  43. backend.add(data);
  44. fireDataAppend(data);
  45. } else {
  46. fireDataUpdate(data);
  47. }
  48. }
  49. @Override
  50. public void remove(T data) {
  51. if (backend.contains(data)) {
  52. backend.remove(data);
  53. fireDataRemove(data);
  54. }
  55. }
  56. @Override
  57. public Iterator<T> iterator() {
  58. return Collections.unmodifiableList(backend).iterator();
  59. }
  60. }