예제 #1
0
  private void initializeWriters() {
    List<EventPropertyDescriptor> writables = new ArrayList<EventPropertyDescriptor>();
    Map<String, Pair<EventPropertyDescriptor, EventPropertyWriter>> writerMap =
        new HashMap<String, Pair<EventPropertyDescriptor, EventPropertyWriter>>();
    writables.addAll(Arrays.asList(underlyingMapType.getWriteableProperties()));

    for (EventPropertyDescriptor writableMapProp : underlyingMapType.getWriteableProperties()) {
      final String propertyName = writableMapProp.getPropertyName();
      writables.add(writableMapProp);
      EventPropertyWriter writer =
          new EventPropertyWriter() {
            public void write(Object value, EventBean target) {
              DecoratingEventBean decorated = (DecoratingEventBean) target;
              decorated.getDecoratingProperties().put(propertyName, value);
            }
          };
      writerMap.put(
          propertyName,
          new Pair<EventPropertyDescriptor, EventPropertyWriter>(writableMapProp, writer));
    }

    if (underlyingEventType instanceof EventTypeSPI) {
      EventTypeSPI spi = (EventTypeSPI) underlyingEventType;
      for (EventPropertyDescriptor writableUndProp : spi.getWriteableProperties()) {
        final String propertyName = writableUndProp.getPropertyName();
        final EventPropertyWriter innerWriter = spi.getWriter(propertyName);
        if (innerWriter == null) {
          continue;
        }

        writables.add(writableUndProp);
        EventPropertyWriter writer =
            new EventPropertyWriter() {
              public void write(Object value, EventBean target) {
                DecoratingEventBean decorated = (DecoratingEventBean) target;
                innerWriter.write(value, decorated.getUnderlyingEvent());
              }
            };
        writerMap.put(
            propertyName,
            new Pair<EventPropertyDescriptor, EventPropertyWriter>(writableUndProp, writer));
      }
    }

    writers = writerMap;
    writableProperties = writables.toArray(new EventPropertyDescriptor[writables.size()]);
  }
예제 #2
0
  public EventBeanWriter getWriter(String[] properties) {
    if (writableProperties == null) {
      initializeWriters();
    }

    boolean isOnlyMap = true;
    for (int i = 0; i < properties.length; i++) {
      if (!writers.containsKey(properties[i])) {
        return null;
      }
      if (underlyingMapType.getWritableProperty(properties[i]) == null) {
        isOnlyMap = false;
      }
    }

    boolean isOnlyUnderlying = true;
    if (!isOnlyMap) {
      EventTypeSPI spi = (EventTypeSPI) underlyingEventType;
      for (int i = 0; i < properties.length; i++) {
        if (spi.getWritableProperty(properties[i]) == null) {
          isOnlyUnderlying = false;
        }
      }
    }

    if (isOnlyMap) {
      return new WrapperEventBeanMapWriter(properties);
    }
    if (isOnlyUnderlying) {
      EventTypeSPI spi = (EventTypeSPI) underlyingEventType;
      EventBeanWriter undWriter = spi.getWriter(properties);
      if (undWriter == null) {
        return undWriter;
      }
      return new WrapperEventBeanUndWriter(undWriter);
    }

    EventPropertyWriter writerArr[] = new EventPropertyWriter[properties.length];
    for (int i = 0; i < properties.length; i++) {
      writerArr[i] = writers.get(properties[i]).getSecond();
    }
    return new WrapperEventBeanPropertyWriter(writerArr);
  }