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

objectos.way.CssUtility Maven / Gradle / Ivy

Go to download

Objectos Way allows you to build full-stack web applications using only Java.

The newest version!
/*
 * Copyright (C) 2023-2024 Objectos 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 objectos.way;

import java.util.Map;
import java.util.Map.Entry;

final class CssUtility implements Css.Rule {

  private final Css.Key key;

  private final String className;

  private final Css.Modifier modifier;

  private final CssProperties properties;

  public CssUtility(Css.Key key, String className, Css.Modifier modifier, CssProperties.Builder properties) {
    this(key, className, modifier, properties.build());
  }

  public CssUtility(Css.Key key, String className, Css.Modifier modifier, CssProperties properties) {
    this.key = key;

    this.className = className;

    this.modifier = modifier;

    this.properties = properties;
  }

  @Override
  public final void accept(Css.Context ctx) {
    Css.Context context;
    context = ctx.contextOf(modifier);

    context.add(this);
  }

  @Override
  public final int compareSameKind(Css.Rule other) {
    CssUtility o;
    o = (CssUtility) other;

    int result;
    result = key.compareTo(o.key);

    if (result != 0) {
      return result;
    }

    return modifier.compareTo(o.modifier);
  }

  @Override
  public final int kind() {
    return 2;
  }

  @Override
  public String toString() {
    StringBuilder out;
    out = new StringBuilder();

    writeTo(out, Css.Indentation.ROOT);

    return out.toString();
  }

  @Override
  public final void writeTo(StringBuilder out, Css.Indentation indentation) {
    indentation.writeTo(out);

    modifier.writeClassName(out, className);

    switch (properties.size()) {
      case 0 -> out.append(" {}");

      case 1 -> {
        Entry prop;
        prop = properties.get(0);

        writeBlockOne(out, prop);
      }

      case 2 -> {
        Entry first;
        first = properties.get(0);

        Entry second;
        second = properties.get(1);

        writeBlockTwo(out, first, second);
      }

      default -> {
        writeBlockMany(out, indentation, properties);
      }
    }

    out.append(System.lineSeparator());
  }

  @Override
  public final void writeProps(StringBuilder out, Css.Indentation indentation) {
    for (Map.Entry property : properties) {
      propertyMany(out, indentation, property);
    }
  }

  private void writeBlockOne(StringBuilder out, Entry property) {
    blockStart(out);

    property(out, property);

    blockEnd(out);
  }

  private void writeBlockTwo(StringBuilder out, Entry prop1, Entry prop2) {
    blockStart(out);

    property(out, prop1);

    nextProperty(out);

    property(out, prop2);

    blockEnd(out);
  }

  private void writeBlockMany(
      StringBuilder out, Css.Indentation indentation, CssProperties properties) {
    blockStartMany(out);

    Css.Indentation next;
    next = indentation.increase();

    for (Map.Entry property : properties) {
      propertyMany(out, next, property);
    }

    blockEndMany(out, indentation);
  }

  private void blockStart(StringBuilder out) {
    out.append(" { ");
  }

  private void blockStartMany(StringBuilder out) {
    out.append(" {");
    out.append(System.lineSeparator());
  }

  private void blockEnd(StringBuilder out) {
    out.append(" }");
  }

  private void blockEndMany(StringBuilder out, Css.Indentation indentation) {
    indentation.writeTo(out);

    out.append('}');
  }

  private void nextProperty(StringBuilder out) {
    out.append("; ");
  }

  private void property(StringBuilder out, Entry property) {
    String name;
    name = property.getKey();

    out.append(name);

    out.append(": ");

    String value;
    value = property.getValue();

    out.append(value);
  }

  private void propertyMany(StringBuilder out, Css.Indentation indentation, Entry property) {
    indentation.writeTo(out);

    property(out, property);

    out.append(';');
    out.append(System.lineSeparator());
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy