Skip to content

Commit

Permalink
DynamicPackageOutputManager and its implementation (#266)
Browse files Browse the repository at this point in the history
* MultiPackageOutputManager and its implementation

* header fix

* checkstyle

* code style fix

* Class rename and correct implementation of the contract.

* formatting

* Renamed interface to DynamicPackageOutputManger

* don't use Override
  • Loading branch information
ratcashdev authored Sep 9, 2022
1 parent 58b9b3e commit 37f1c54
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2014-2022 Real Logic Limited.
*
* 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
*
* https://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.agrona.generation;

/**
* Extended version of the OutputManager allowing to specify packages for selected outputs.
*/
public interface DynamicPackageOutputManger extends OutputManager
{
/**
* Sets the package name to be used by the Writer obtained through the very next call to {@link
* #createOutput(java.lang.String) }. A subsequent call to
* {@link #createOutput(java.lang.String) } should use the default package name,
* whatever that is.
*
* @param packageName the packageName
*/
void setPackageName(String packageName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.agrona.generation;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
Expand All @@ -23,9 +25,10 @@
/**
* An {@link OutputManager} which can store source files as {@link StringWriter} buy source file name.
*/
public class StringWriterOutputManager implements OutputManager
public class StringWriterOutputManager implements DynamicPackageOutputManger
{
private String packageName;
private String basePackageName;
private final HashMap<String, StringWriter> sourceFileByName = new HashMap<>();

/**
Expand All @@ -36,7 +39,14 @@ public Writer createOutput(final String name)
final StringWriter stringWriter = new StringWriter();
sourceFileByName.put(packageName + "." + name, stringWriter);

return stringWriter;
return new FilterWriter(stringWriter)
{
public void close() throws IOException
{
super.close();
packageName = basePackageName;
}
};
}

/**
Expand All @@ -47,6 +57,10 @@ public Writer createOutput(final String name)
public void setPackageName(final String packageName)
{
this.packageName = packageName;
if (basePackageName == null)
{
basePackageName = packageName;
}
}

/**
Expand Down

0 comments on commit 37f1c54

Please sign in to comment.