/plugins/org.integratedmodelling.thinklab.clojure/src/org/integratedmodelling/clojure/utils/OptionListIterator.java

https://github.com/ariesteam/thinklab · Java · 65 lines · 30 code · 9 blank · 26 comment · 4 complexity · 41be22277d8f07c0866bb99f967bbf81 MD5 · raw file

  1. /**
  2. * Copyright 2011 The ARIES Consortium (http://www.ariesonline.org) and
  3. * www.integratedmodelling.org.
  4. This file is part of Thinklab.
  5. Thinklab is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published
  7. by the Free Software Foundation, either version 3 of the License,
  8. or (at your option) any later version.
  9. Thinklab is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Thinklab. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. package org.integratedmodelling.clojure.utils;
  17. import java.util.Collection;
  18. import java.util.Iterator;
  19. import org.integratedmodelling.thinklab.exception.ThinklabRuntimeException;
  20. import org.integratedmodelling.utils.Pair;
  21. /**
  22. * Pass a collection (e.g. a clojure list of :kw value pairs) and retrieve pairs of key/value with the key
  23. * converted to a string and the leading colon removed. Also performs minimal error checking and is null-tolerant.
  24. *
  25. * @author Ferdinando
  26. *
  27. */
  28. public class OptionListIterator implements Iterator<Pair<String, Object>> {
  29. Iterator<?> _it = null;
  30. public OptionListIterator(Object o) {
  31. if (o != null)
  32. _it = ((Collection<?>)o).iterator();
  33. }
  34. @Override
  35. public boolean hasNext() {
  36. return _it == null ? false : _it.hasNext();
  37. }
  38. @Override
  39. public Pair<String, Object> next() {
  40. String key = _it.next().toString();
  41. Object val = _it.next();
  42. if (!key.startsWith(":"))
  43. throw new ThinklabRuntimeException("keyword list improperly formatted: key is not a clojure keyword");
  44. key = key.substring(1);
  45. return new Pair<String, Object>(key, val);
  46. }
  47. @Override
  48. public void remove() {
  49. _it.remove();
  50. _it.remove();
  51. }
  52. }