001/* 002 Copyright (c) 2012, 2015, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag. 003 004 Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 use this file except in compliance with the License. You may obtain a copy of 006 the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010 Unless required by applicable law or agreed to in writing, software 011 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 License for the specific language governing permissions and limitations under 014 the License. 015 */ 016package org.javamoney.moneta.spi; 017 018import java.net.URI; 019import java.util.Map; 020 021import org.javamoney.moneta.spi.LoaderService.LoaderListener; 022import org.javamoney.moneta.spi.LoaderService.UpdatePolicy; 023 024/** 025 * Builder to {@link LoadDataInformation} 026 * Programmatically registers a remote resource {@code resourceLocation}, 027 * backed up by a classpath resource {@code backupResource}, reachable as 028 * {@code dataId}. 029 */ 030public class LoadDataInformationBuilder { 031 /** 032 * The unique identifier of the resource that must also be used 033 * for accessing the resource, not {@code null}. 034 */ 035 private String resourceId; 036 /** 037 * The policy, when the dta has to be reloaded/renewed. 038 */ 039 private UpdatePolicy updatePolicy; 040 041 private Map<String, String> properties; 042 043 /** 044 * An (optional) LoaderListener to be registered. 045 */ 046 private LoaderListener loaderListener; 047 /** 048 * The backup resource location in the classpath, not 049 * {@code null}. 050 */ 051 private URI backupResource; 052 /** The remote resource locations, not {@code null}. */ 053 private URI[] resourceLocations; 054 055 private boolean startRemote; 056 057 public LoadDataInformationBuilder withResourceId(String resourceId) { 058 this.resourceId = resourceId; 059 return this; 060 } 061 062 public LoadDataInformationBuilder withUpdatePolicy(UpdatePolicy updatePolicy) { 063 this.updatePolicy = updatePolicy; 064 return this; 065 } 066 067 public LoadDataInformationBuilder withProperties(Map<String, String> properties) { 068 this.properties = properties; 069 return this; 070 } 071 072 public LoadDataInformationBuilder withLoaderListener(LoaderListener loaderListener) { 073 this.loaderListener = loaderListener; 074 return this; 075 } 076 077 public LoadDataInformationBuilder withBackupResource(URI backupResource) { 078 this.backupResource = backupResource; 079 return this; 080 } 081 082 public LoadDataInformationBuilder withResourceLocations(URI... resourceLocations) { 083 this.resourceLocations = resourceLocations; 084 return this; 085 } 086 087 public LoadDataInformationBuilder withStartRemote(boolean startRemote) { 088 this.startRemote = startRemote; 089 return this; 090 } 091 092 public LoadDataInformation build() { 093 if(resourceId==null || resourceId.isEmpty()) { 094 throw new IllegalStateException("The resourceId should be informed"); 095 } 096 else if (updatePolicy==null) { 097 throw new IllegalStateException("The updatePolicy should be informed"); 098 } 099 else if (properties==null) { 100 throw new IllegalStateException("The properties should be informed"); 101 } 102 else if (resourceLocations==null) { 103 throw new IllegalStateException("The properties should be informed"); 104 } 105 return new LoadDataInformation(resourceId, updatePolicy, properties, 106 loaderListener, backupResource, resourceLocations, startRemote); 107 } 108 109 110}