All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.css.io.IdSelector Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.css.io;

import java.util.Objects;

import br.com.objectos.codereader.CodeReader;
import br.com.objectos.css.Selectable;

/**
 * @author [email protected] (Marcio Endo)
 */
class IdSelector implements AbstractSelector {

  private static final SelectorAction ACTION = SingleSelectorAction.of('#', IdSelector::parse);

  private final String id;

  public IdSelector(String id) {
    this.id = id;
  }

  public static SelectorAction action() {
    return ACTION;
  }

  public static IdSelector parse(CodeReader reader) {
    while (reader.hasNext()) {
      reader.consume();
      if (!CssKeywords.identifierChar().peek(reader)) {
        break;
      }
    }
    String id = reader.get();
    return new IdSelector(id);
  }

  @Override
  public final boolean equals(Object obj) {
    if (!(obj instanceof IdSelector)) {
      return false;
    }
    return ((IdSelector) obj).id.equals(id);
  }

  @Override
  public final int hashCode() {
    return id.hashCode();
  }

  @Override
  public boolean matches(Selectable element) {
    String attributeValue = element.attributeValue("id");
    return Objects.equals(attributeValue, id);
  }

  @Override
  public final String toString() {
    return "#" + id;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy