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

org.apache.geode.internal.util.TransformUtils Maven / Gradle / Ivy

Go to download

Apache Geode provides a database-like consistency model, reliable transaction processing and a shared-nothing architecture to maintain very low latency performance with high concurrency processing

There is a newer version: 1.15.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You 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 org.apache.geode.internal.util;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.geode.internal.cache.persistence.PersistentMemberID;

/**
 * Contains common data tranformation utility methods and transformers.
 */
public final class TransformUtils {

  /**
   * Transforms PersistentMemberIDs to a user friendly log entry.
   */
  public final static Transformer>, String> persistentMemberEntryToLogEntryTransformer =
      new Transformer>, String>() {
        @Override
        public String transform(Map.Entry> entry) {
          PersistentMemberID memberId = entry.getKey();
          Set bucketIds = entry.getValue();
          StringBuilder builder = new StringBuilder();
          builder.append(persistentMemberIdToLogEntryTransformer.transform(memberId));

          if (null != bucketIds) {
            builder.append("  Buckets: ");
            builder.append(bucketIds);
          }

          builder.append("\n");

          return builder.toString();
        }
      };

  /**
   * Transforms PersistentMemberIDs to a user friendly log entry.
   */
  public final static Transformer persistentMemberIdToLogEntryTransformer =
      new Transformer() {
        @Override
        public String transform(PersistentMemberID memberId) {
          StringBuilder builder = new StringBuilder();

          if (null != memberId) {
            if (null != memberId.diskStoreId) {
              builder.append("\n  DiskStore ID: ");
              builder.append(memberId.diskStoreId.toUUID().toString());
            }

            if (null != memberId.name) {
              builder.append("\n  Name: ");
              builder.append(memberId.name);
            }

            if ((null != memberId.host) && (null != memberId.directory)) {
              builder.append("\n  Location: ");
            }

            if (null != memberId.host) {
              builder.append("/");
              builder.append(memberId.host.getHostAddress());
              builder.append(":");
            }

            if (null != memberId.directory) {
              builder.append(memberId.directory);
            }

            builder.append("\n");
          }

          return builder.toString();
        }
      };

  /**
   * This is a simple file to file name transformer.
   */
  public final static Transformer fileNameTransformer =
      new Transformer() {
        public String transform(File file) {
          return file.getName();
        }
      };

  /**
   * Transforms a collection of one data type into another.
   * 
   * @param from a collection of data to be transformed.
   * @param to a collection to contain the transformed data.
   * @param transformer transforms the data.
   */
  public static  void transform(Collection from, Collection to,
      Transformer transformer) {
    for (T1 instance : from) {
      to.add(transformer.transform(instance));
    }
  }

  /**
   * Transforms a collection of one data type into another and returns a map using the transformed
   * type as the key and the original type as the value.
   * 
   * @param from a collection of data to be transformed.
   * @param transformer transforms the data.
   * 
   * @return a Map of transformed values that are keys to the original values.
   */
  public static  Map transformAndMap(Collection from,
      Transformer transformer) {
    Map map = new HashMap();
    for (T1 instance : from) {
      map.put(transformer.transform(instance), instance);
    }

    return map;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy