001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang; 020 021import org.apache.reef.tang.formats.AvroConfigurationSerializer; 022import org.apache.reef.tang.formats.ConfigurationSerializer; 023 024/** 025 * Helper class for Configurations. 026 */ 027public final class Configurations { 028 029 private static final AvroConfigurationSerializer SERIALIZER = new AvroConfigurationSerializer(); 030 031 /** 032 * This is a utility class that isn't meant to be instantiated. 033 */ 034 private Configurations() { 035 } 036 037 /** 038 * Merge a set of Configurations. 039 * 040 * @param configurations the configuration to be merged 041 * @return the merged configuration. 042 * @throws org.apache.reef.tang.exceptions.BindException if the merge fails. 043 */ 044 public static Configuration merge(final Configuration... configurations) { 045 return Tang.Factory.getTang().newConfigurationBuilder(configurations).build(); 046 } 047 048 /** 049 * Merge a set of Configurations. 050 * 051 * @param configurations the configuration to be merged 052 * @return the merged configuration. 053 * @throws org.apache.reef.tang.exceptions.BindException if the merge fails. 054 */ 055 public static Configuration merge(final Iterable<Configuration> configurations) { 056 final ConfigurationBuilder configurationBuilder = Tang.Factory.getTang().newConfigurationBuilder(); 057 for (final Configuration configuration : configurations) { 058 configurationBuilder.addConfiguration(configuration); 059 } 060 return configurationBuilder.build(); 061 } 062 063 /** 064 * Get the default configuration serializer. 065 * Currently it is AvroConfigurationSerializer. 066 * Use Tang.toString(...) to produce a human-readable string representation of the config. 067 * @return configuration serializer object. 068 */ 069 public static ConfigurationSerializer getDefaultSerializer() { 070 return SERIALIZER; 071 } 072 073 /** 074 * Return human-readable representation of the configuration. 075 * @param config input configuration. 076 * @return a string that contains human-readable representation of the input configuration. 077 */ 078 public static String toString(final Configuration config) { 079 return SERIALIZER.toString(config); 080 } 081 082 /** 083 * Return human-readable representation of the configuration. 084 * If prettyPrint is true, try to produce a nicer text layout, if possible. 085 * @param config input configuration. 086 * @param prettyPrint if true, try to produce a nicer text layout, when possible. 087 * Otherwise, use the default options of the serializer. 088 * Default value is false as not all serializers and formats support pretty printing. 089 * @return a string that contains human-readable representation of the input configuration. 090 */ 091 public static String toString(final Configuration config, final boolean prettyPrint) { 092 return SERIALIZER.toString(config, prettyPrint); 093 } 094}