2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.dynamodb.internal;
16 * The DynamoDBTableNameResolver resolves DynamoDB table name for a given item.
18 * @author Sami Salonen - Initial contribution
21 public class DynamoDBTableNameResolver {
23 private final String tablePrefix;
25 public DynamoDBTableNameResolver(String tablePrefix) {
26 this.tablePrefix = tablePrefix;
29 public String fromItem(DynamoDBItem<?> item) {
30 final String[] tableName = new String[1];
32 // Use the visitor pattern to deduce the table name
33 item.accept(new DynamoDBItemVisitor() {
36 public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
37 tableName[0] = tablePrefix + "bigdecimal";
41 public void visit(DynamoDBStringItem dynamoStringItem) {
42 tableName[0] = tablePrefix + "string";
49 * Construct DynamoDBTableNameResolver corresponding to DynamoDBItem class
54 public String fromClass(Class<? extends DynamoDBItem<?>> clazz) {
55 DynamoDBItem<?> dummy;
57 // Construct new instance of this class (assuming presense no-argument constructor)
58 // in order to re-use fromItem(DynamoDBItem) constructor
59 dummy = clazz.getConstructor().newInstance();
60 } catch (Exception e) {
61 throw new IllegalStateException(String.format("Could not find suitable constructor for class %s", clazz));
63 return this.fromItem(dummy);