Attributes.java

  1. /*
  2.  * Copyright (C) 2015, Ivan Motsch <ivan.motsch@bsiag.com>,
  3.  * Copyright (C) 2017, Obeo (mathieu.cartaud@obeo.fr)
  4.  *
  5.  * This program and the accompanying materials are made available
  6.  * under the terms of the Eclipse Distribution License v1.0 which
  7.  * accompanies this distribution, is reproduced below, and is
  8.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  9.  *
  10.  * All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or
  13.  * without modification, are permitted provided that the following
  14.  * conditions are met:
  15.  *
  16.  * - Redistributions of source code must retain the above copyright
  17.  *   notice, this list of conditions and the following disclaimer.
  18.  *
  19.  * - Redistributions in binary form must reproduce the above
  20.  *   copyright notice, this list of conditions and the following
  21.  *   disclaimer in the documentation and/or other materials provided
  22.  *   with the distribution.
  23.  *
  24.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  25.  *   names of its contributors may be used to endorse or promote
  26.  *   products derived from this software without specific prior
  27.  *   written permission.
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  */

  43. package org.eclipse.jgit.attributes;

  44. import java.util.ArrayList;
  45. import java.util.Collection;
  46. import java.util.LinkedHashMap;
  47. import java.util.Map;

  48. import org.eclipse.jgit.attributes.Attribute.State;
  49. import org.eclipse.jgit.lib.Constants;

  50. /**
  51.  * Represents a set of attributes for a path
  52.  *
  53.  * @since 4.2
  54.  */
  55. public final class Attributes {
  56.     private final Map<String, Attribute> map = new LinkedHashMap<>();

  57.     /**
  58.      * Creates a new instance
  59.      *
  60.      * @param attributes
  61.      *            a {@link org.eclipse.jgit.attributes.Attribute}
  62.      */
  63.     public Attributes(Attribute... attributes) {
  64.         if (attributes != null) {
  65.             for (Attribute a : attributes) {
  66.                 put(a);
  67.             }
  68.         }
  69.     }

  70.     /**
  71.      * Whether the set of attributes is empty
  72.      *
  73.      * @return true if the set does not contain any attributes
  74.      */
  75.     public boolean isEmpty() {
  76.         return map.isEmpty();
  77.     }

  78.     /**
  79.      * Get the attribute with the given key
  80.      *
  81.      * @param key
  82.      *            a {@link java.lang.String} object.
  83.      * @return the attribute or null
  84.      */
  85.     public Attribute get(String key) {
  86.         return map.get(key);
  87.     }

  88.     /**
  89.      * Get all attributes
  90.      *
  91.      * @return all attributes
  92.      */
  93.     public Collection<Attribute> getAll() {
  94.         return new ArrayList<>(map.values());
  95.     }

  96.     /**
  97.      * Put an attribute
  98.      *
  99.      * @param a
  100.      *            an {@link org.eclipse.jgit.attributes.Attribute}
  101.      */
  102.     public void put(Attribute a) {
  103.         map.put(a.getKey(), a);
  104.     }

  105.     /**
  106.      * Remove attribute with given key
  107.      *
  108.      * @param key
  109.      *            an attribute name
  110.      */
  111.     public void remove(String key) {
  112.         map.remove(key);
  113.     }

  114.     /**
  115.      * Whether there is an attribute with this key
  116.      *
  117.      * @param key
  118.      *            key of an attribute
  119.      * @return true if the {@link org.eclipse.jgit.attributes.Attributes}
  120.      *         contains this key
  121.      */
  122.     public boolean containsKey(String key) {
  123.         return map.containsKey(key);
  124.     }

  125.     /**
  126.      * Return the state.
  127.      *
  128.      * @param key
  129.      *            key of an attribute
  130.      * @return the state (never returns <code>null</code>)
  131.      */
  132.     public Attribute.State getState(String key) {
  133.         Attribute a = map.get(key);
  134.         return a != null ? a.getState() : Attribute.State.UNSPECIFIED;
  135.     }

  136.     /**
  137.      * Whether the attribute is set
  138.      *
  139.      * @param key
  140.      *            a {@link java.lang.String} object.
  141.      * @return true if the key is
  142.      *         {@link org.eclipse.jgit.attributes.Attribute.State#SET}, false in
  143.      *         all other cases
  144.      */
  145.     public boolean isSet(String key) {
  146.         return (getState(key) == State.SET);
  147.     }

  148.     /**
  149.      * Whether the attribute is unset
  150.      *
  151.      * @param key
  152.      *            a {@link java.lang.String} object.
  153.      * @return true if the key is
  154.      *         {@link org.eclipse.jgit.attributes.Attribute.State#UNSET}, false
  155.      *         in all other cases
  156.      */
  157.     public boolean isUnset(String key) {
  158.         return (getState(key) == State.UNSET);
  159.     }

  160.     /**
  161.      * Whether the attribute with the given key is unspecified
  162.      *
  163.      * @param key
  164.      *            a {@link java.lang.String} object.
  165.      * @return true if the key is
  166.      *         {@link org.eclipse.jgit.attributes.Attribute.State#UNSPECIFIED},
  167.      *         false in all other cases
  168.      */
  169.     public boolean isUnspecified(String key) {
  170.         return (getState(key) == State.UNSPECIFIED);
  171.     }

  172.     /**
  173.      * Is this a custom attribute
  174.      *
  175.      * @param key
  176.      *            a {@link java.lang.String} object.
  177.      * @return true if the key is
  178.      *         {@link org.eclipse.jgit.attributes.Attribute.State#CUSTOM}, false
  179.      *         in all other cases see {@link #getValue(String)} for the value of
  180.      *         the key
  181.      */
  182.     public boolean isCustom(String key) {
  183.         return (getState(key) == State.CUSTOM);
  184.     }

  185.     /**
  186.      * Get attribute value
  187.      *
  188.      * @param key
  189.      *            an attribute key
  190.      * @return the attribute value (may be <code>null</code>)
  191.      */
  192.     public String getValue(String key) {
  193.         Attribute a = map.get(key);
  194.         return a != null ? a.getValue() : null;
  195.     }

  196.     /**
  197.      * Test if the given attributes implies to handle the related entry as a
  198.      * binary file (i.e. if the entry has an -merge or a merge=binary attribute)
  199.      * or if it can be content merged.
  200.      *
  201.      * @return <code>true</code> if the entry can be content merged,
  202.      *         <code>false</code> otherwise
  203.      * @since 4.9
  204.      */
  205.     public boolean canBeContentMerged() {
  206.         if (isUnset(Constants.ATTR_MERGE)) {
  207.             return false;
  208.         } else if (isCustom(Constants.ATTR_MERGE)
  209.                 && getValue(Constants.ATTR_MERGE)
  210.                         .equals(Constants.ATTR_BUILTIN_BINARY_MERGER)) {
  211.             return false;
  212.         }
  213.         return true;
  214.     }

  215.     /** {@inheritDoc} */
  216.     @Override
  217.     public String toString() {
  218.         StringBuilder buf = new StringBuilder();
  219.         buf.append(getClass().getSimpleName());
  220.         buf.append("["); //$NON-NLS-1$
  221.         buf.append(" "); //$NON-NLS-1$
  222.         for (Attribute a : map.values()) {
  223.             buf.append(a.toString());
  224.             buf.append(" "); //$NON-NLS-1$
  225.         }
  226.         buf.append("]"); //$NON-NLS-1$
  227.         return buf.toString();
  228.     }

  229.     /** {@inheritDoc} */
  230.     @Override
  231.     public int hashCode() {
  232.         return map.hashCode();
  233.     }

  234.     /** {@inheritDoc} */
  235.     @Override
  236.     public boolean equals(Object obj) {
  237.         if (this == obj)
  238.             return true;
  239.         if (!(obj instanceof Attributes))
  240.             return false;
  241.         Attributes other = (Attributes) obj;
  242.         return this.map.equals(other.map);
  243.     }

  244. }