-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marshall Hampson
committed
Nov 15, 2018
1 parent
1dbd084
commit 88cb239
Showing
7 changed files
with
124 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Record.java | ||
* Created on Nov 15, 2018, 11:41 AM | ||
* | ||
* Copyright 2008-2018 LiveAction, Incorporated. All Rights Reserved. | ||
* 3500 W Bayshore Road, Palo Alto, California 94303, U.S.A. | ||
* | ||
* This software is the confidential and proprietary information | ||
* of LiveAction ("Confidential Information"). | ||
* You shall not disclose such Confidential Information and shall use | ||
* it only in accordance with the terms of the license agreement | ||
* you entered into with LiveAction. | ||
*/ | ||
package com.mrhampson.database; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.*; | ||
|
||
/** | ||
* @author Marshall Hampson | ||
*/ | ||
public class Record { | ||
private final int recordBytes; | ||
private final Map<String, ColumnValue<?>> columnValues; | ||
|
||
private Record(Builder builder) { | ||
this.recordBytes = builder.recordBytes; | ||
this.columnValues = Collections.unmodifiableMap(builder.values); | ||
} | ||
|
||
public Map<String, ColumnValue<?>> getColumnValues() { | ||
return this.columnValues; | ||
} | ||
|
||
public int getRecordBytes() { | ||
return recordBytes; | ||
} | ||
|
||
public byte[] toBytes() { | ||
ByteBuffer buffer = ByteBuffer.allocate(recordBytes); | ||
for (ColumnValue<?> value : columnValues.values()) { | ||
buffer.put(value.toBytes()); | ||
} | ||
return buffer.array(); | ||
} | ||
|
||
public static final class Builder { | ||
private final int recordBytes; | ||
private final TableDefinition tableDefinition; | ||
private final Map<String, ColumnValue<?>> values; | ||
|
||
public Builder(TableDefinition tableDefinition) { | ||
Objects.requireNonNull(tableDefinition); | ||
this.tableDefinition = tableDefinition; | ||
this.values = new LinkedHashMap<>(); | ||
int byteCounter = 0; | ||
for (ColumnDefinition columnDefinition : this.tableDefinition.getColumns()) { | ||
byteCounter += columnDefinition.getFieldLength(); | ||
this.values.put(columnDefinition.getColumnName(), ColumnValue.fromColumnDefinition(columnDefinition)); | ||
} | ||
this.recordBytes = byteCounter; | ||
} | ||
|
||
public void setColumnValue(String columnName, Object value) { | ||
Objects.requireNonNull(columnName); | ||
ColumnValue<?> columnValue = this.values.get(columnName); | ||
if (columnValue == null) { | ||
throw new IllegalArgumentException("Column not defined"); | ||
} | ||
columnValue.setValue(value); | ||
} | ||
|
||
public Record build() { | ||
return new Record(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters