PageRenderTime 224ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/shell/src/main/java/org/jboss/forge/shell/resources/ResourceEventGroomer.java

https://github.com/includeeasy/core
Java | 104 lines | 67 code | 11 blank | 26 comment | 3 complexity | c3cad231da0c6e6d61ce7fbe6d41fe17 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.forge.shell.resources;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import org.jboss.forge.bus.spi.EventBusGroomer;
  26. import org.jboss.forge.resources.Resource;
  27. import org.jboss.forge.resources.VirtualResource;
  28. import org.jboss.forge.resources.events.ResourceEvent;
  29. /**
  30. * Publishes file change events to the shell output stream.
  31. *
  32. * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
  33. */
  34. public class ResourceEventGroomer implements EventBusGroomer
  35. {
  36. @Override
  37. public List<Object> groom(final List<Object> events)
  38. {
  39. List<Resource<?>> seen = new ArrayList<Resource<?>>();
  40. List<Object> result = new ArrayList<Object>();
  41. for (Object e : events) {
  42. if (e instanceof ResourceEvent)
  43. {
  44. Resource<?> resource = ((ResourceEvent) e).getResource();
  45. if (seen.contains(resource))
  46. {
  47. result.remove(seen.indexOf(resource));
  48. seen.remove(seen.indexOf(resource));
  49. }
  50. result.add(e);
  51. seen.add(resource);
  52. }
  53. else
  54. {
  55. result.add(e);
  56. seen.add(new Placeholder(null));
  57. }
  58. }
  59. return result;
  60. }
  61. private final class Placeholder extends VirtualResource<Object>
  62. {
  63. private Placeholder(final Resource<?> parent)
  64. {
  65. super(null);
  66. }
  67. @Override
  68. public boolean delete() throws UnsupportedOperationException
  69. {
  70. return true;
  71. }
  72. @Override
  73. public boolean delete(final boolean recursive) throws UnsupportedOperationException
  74. {
  75. return true;
  76. }
  77. @Override
  78. public String getName()
  79. {
  80. return "";
  81. }
  82. @Override
  83. public List<Resource<?>> listResources()
  84. {
  85. return new ArrayList<Resource<?>>();
  86. }
  87. @Override
  88. public Object getUnderlyingResourceObject()
  89. {
  90. return new Object();
  91. }
  92. }
  93. }