PageRenderTime 24ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/pmuir/forge-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 */
 22package org.jboss.forge.shell.resources;
 23
 24import java.util.ArrayList;
 25import java.util.List;
 26
 27import org.jboss.forge.bus.spi.EventBusGroomer;
 28import org.jboss.forge.resources.Resource;
 29import org.jboss.forge.resources.VirtualResource;
 30import org.jboss.forge.resources.events.ResourceEvent;
 31
 32/**
 33 * Publishes file change events to the shell output stream.
 34 * 
 35 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 36 */
 37public class ResourceEventGroomer implements EventBusGroomer
 38{
 39   @Override
 40   public List<Object> groom(final List<Object> events)
 41   {
 42      List<Resource<?>> seen = new ArrayList<Resource<?>>();
 43      List<Object> result = new ArrayList<Object>();
 44
 45      for (Object e : events) {
 46         if (e instanceof ResourceEvent)
 47         {
 48            Resource<?> resource = ((ResourceEvent) e).getResource();
 49            if (seen.contains(resource))
 50            {
 51               result.remove(seen.indexOf(resource));
 52               seen.remove(seen.indexOf(resource));
 53            }
 54            result.add(e);
 55            seen.add(resource);
 56         }
 57         else
 58         {
 59            result.add(e);
 60            seen.add(new Placeholder(null));
 61         }
 62      }
 63
 64      return result;
 65   }
 66
 67   private final class Placeholder extends VirtualResource<Object>
 68   {
 69      private Placeholder(final Resource<?> parent)
 70      {
 71         super(null);
 72      }
 73
 74      @Override
 75      public boolean delete() throws UnsupportedOperationException
 76      {
 77         return true;
 78      }
 79
 80      @Override
 81      public boolean delete(final boolean recursive) throws UnsupportedOperationException
 82      {
 83         return true;
 84      }
 85
 86      @Override
 87      public String getName()
 88      {
 89         return "";
 90      }
 91
 92      @Override
 93      public List<Resource<?>> listResources()
 94      {
 95         return new ArrayList<Resource<?>>();
 96      }
 97
 98      @Override
 99      public Object getUnderlyingResourceObject()
100      {
101         return new Object();
102      }
103   }
104}