From ee6ff1f926fd36913e5fc4e6986d91bfc0e0e109 Mon Sep 17 00:00:00 2001 From: daneps Date: Thu, 25 Jan 2018 10:46:24 -0500 Subject: [PATCH] Update FormatterChain to use a linked list This should solve the issue of the formatter chain only working for the first variable by using a pointer to the next chain --- .../handlebars/internal/FormatterChain.java | 73 +++++++++++-------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/handlebars/src/main/java/com/github/jknack/handlebars/internal/FormatterChain.java b/handlebars/src/main/java/com/github/jknack/handlebars/internal/FormatterChain.java index 8a009e268..add1aa306 100644 --- a/handlebars/src/main/java/com/github/jknack/handlebars/internal/FormatterChain.java +++ b/handlebars/src/main/java/com/github/jknack/handlebars/internal/FormatterChain.java @@ -1,14 +1,14 @@ /** * Copyright (c) 2012-2015 Edgar Espina - * + *

* This file is part of Handlebars.java. - * + *

* 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 - * + *

+ * 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. @@ -17,12 +17,13 @@ */ package com.github.jknack.handlebars.internal; -import static org.apache.commons.lang3.Validate.notNull; +import com.github.jknack.handlebars.Formatter; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import com.github.jknack.handlebars.Formatter; +import static org.apache.commons.lang3.Validate.notNull; /** * Default implementation for formatter chain. @@ -32,29 +33,41 @@ */ public class FormatterChain implements Formatter.Chain { - /** Pointer to next formatter. */ - private Iterator chain; - - /** - * Creates a new {@link FormatterChain}. - * - * @param formatter List of available formatters. - */ - public FormatterChain(final List formatter) { - this.chain = formatter.iterator(); - } - - @Override - public Object format(final Object value) { - Object output; - if (chain.hasNext()) { - Formatter formatter = chain.next(); - output = formatter.format(value, this); - notNull(output, "Formatter " + formatter.getClass() + " returned a null result for " + value); - } else { - output = value.toString(); + /** Pointer to next formatter Chain. */ + private Formatter.Chain nextFormatterChain; + /** Pointer to formatter. */ + private Formatter formatter; + + /** + * Creates a new {@link FormatterChain}. + * + * @param formatters List of available formatters. + */ + public FormatterChain(final List formatters) { + List formattersToAdd = new ArrayList(formatters); + Iterator formatIter = formattersToAdd.iterator(); + if (formatIter.hasNext()) { + this.formatter = formatIter.next(); + formatIter.remove(); + if (formatIter.hasNext()) { + nextFormatterChain = new FormatterChain(formattersToAdd); + } else { + nextFormatterChain = Formatter.NOOP; + } + } + } + + @Override + public Object format(final Object value) { + Object output; + if (formatter != null) { + output = formatter.format(value, this.nextFormatterChain); + notNull(output, "Formatter " + formatter.getClass() + " returned a null result for " + + value); + } else { + output = value.toString(); + } + return output; } - return output; - } }