/*
 * Copyright (c) 2011 Google Inc.
 *
 * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.google.eclipse.protobuf.model.util;

import static java.util.Collections.unmodifiableList;

import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import com.google.eclipse.protobuf.protobuf.Message;
import com.google.eclipse.protobuf.protobuf.MessageElement;
import com.google.eclipse.protobuf.protobuf.MessageField;
import com.google.eclipse.protobuf.protobuf.OneOf;
import com.google.eclipse.protobuf.protobuf.Protobuf;
import com.google.eclipse.protobuf.protobuf.TypeExtension;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.emf.ecore.EObject;

/**
 * Utility methods related to <code>{@link Message}</code>s.
 *
 * @author alruiz@google.com (Alex Ruiz)
 */
@Singleton public class Messages {
  @Inject private ModelObjects modelObjects;
  @Inject private TypeExtensions typeExtensions;

  /**
   * Returns all the extensions of the given message declared in the same file as the message.
   * @param message the given message.
   * @return all the extensions of the given message declared in the same file as the message, or an empty collection if
   * none are found.
   */
  public Collection<TypeExtension> localExtensionsOf(Message message) {
    return extensionsOf(message, modelObjects.rootOf(message));
  }

  public Collection<TypeExtension> extensionsOf(Message message, Protobuf root) {
    Set<TypeExtension> extensions = newHashSet();
    for (TypeExtension extension : getAllContentsOfType(root, TypeExtension.class)) {
      Message referred = typeExtensions.messageFrom(extension);
      if (message.equals(referred)) {
        extensions.add(extension);
      }
    }
    return extensions;
  }

  /**
   * Returns all the fields of the given <code>{@link Message}</code>.
   * @param message the given message.
   * @return all the fields of the given {@code Message}.
   */
  public Collection<MessageField> fieldsOf(Message message) {
    List<MessageField> fields = newArrayList();
    fieldsOf(message, fields);
    return unmodifiableList(fields);
  }

  private void fieldsOf(EObject message, List<MessageField> fields) {
    if (message instanceof Message) {
      for (MessageElement e : ((Message) message).getElements()) {
        if (e instanceof MessageField) {
          fields.add((MessageField) e);
        }
      }
    } else if (message instanceof OneOf) {
      for (MessageElement e : ((OneOf) message).getElements()) {
        if (e instanceof MessageField) {
          fields.add((MessageField) e);
        }
      }
    }
  }
}

@@ -642,24 +642,114 @@
 .util.Set;%0A%0A
+import com.google.common.base.Strings;%0Aimport com.google.eclipse.protobuf.protobuf.Group;%0A
 import com.g
@@ -2251,862 +2251,7 @@
 %0A  %7D
-%0A%0A  /**%0A   * Returns all the fields of the given %3Ccode%3E%7B@link Message%7D%3C/code%3E.%0A   * @param message the given message.%0A   * @return all the fields of the given %7B@code Message%7D.%0A   */%0A  public Collection%3CMessageField%3E fieldsOf(Message message) %7B%0A    List%3CMessageField%3E fields = newArrayList();%0A    fieldsOf(message, fields);%0A    return unmodifiableList(fields);%0A  %7D%0A%0A  private void fieldsOf(EObject message, List%3CMessageField%3E fields) %7B%0A    if (message instanceof Message) %7B%0A      for (MessageElement e : ((Message) message).getElements()) %7B%0A        if (e instanceof MessageField) %7B%0A          fields.add((MessageField) e);%0A        %7D%0A      %7D%0A    %7D else if (message instanceof OneOf) %7B%0A      for (MessageElement e : ((OneOf) message).getElements()) %7B%0A        if (e instanceof MessageField) %7B%0A          fields.add((MessageField) e);%0A        %7D%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

Remove unused functions