All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.recon.ReconFormWriter Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
// Copyright 2015-2019 SWIM.AI inc.
//
// 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
//
// 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 swim.recon;

import swim.codec.Output;
import swim.codec.Writer;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Value;

final class ReconFormWriter extends Writer {
  final ReconWriter recon;
  final Form form;
  final T object;
  final Writer part;

  ReconFormWriter(ReconWriter recon, Form form, T object, Writer part) {
    this.recon = recon;
    this.form = form;
    this.object = object;
    this.part = part;
  }

  ReconFormWriter(ReconWriter recon, Form form) {
    this(recon, form, null, null);
  }

  @Override
  public Writer feed(T object) {
    return new ReconFormWriter(recon, form, object, null);
  }

  @Override
  public Writer pull(Output output) {
    return write(output, this.recon, this.form, this.object, this.part);
  }

  static  Writer write(Output output, ReconWriter recon,
                                Form form, T object, Writer part) {
    if (output == null) {
      return done();
    }
    if (part == null) {
      final Value value = form.mold(object).toValue();
      part = recon.writeValue(value, output);
    } else {
      part = part.pull(output);
    }
    if (part.isDone()) {
      return done(object);
    } else if (part.isError()) {
      return part.asError();
    }
    return new ReconFormWriter(recon, form, object, part);
  }

  static  Writer write(Output output, ReconWriter recon,
                                Form form, T object) {
    return write(output, recon, form, object, null);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy